1、Frameworkを追加
#import <AssetsLibrary/AssetsLibrary.h>
#import <ImageIO/ImageIO.h>
2、サンプルコード
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary*)info {
[picker dismissModalViewControllerAnimated:NO];
// カメラロールからiPhoneカメラで撮った場合、GPSなど取得可能
NSURL *url = [info objectForKey:UIImagePickerControllerReferenceURL];
ALAssetsLibrary *lib = [[[ALAssetsLibrary alloc] init] autorelease];
NSLog(@">>>>>> %@", url);
[lib assetForURL:url
resultBlock:^(ALAsset *asset){
ALAssetRepresentation *representation = [asset defaultRepresentation];
NSUInteger size = [representation size];
uint8_t *buff = (uint8_t *)malloc(sizeof(uint8_t)*size);
if(buff != nil){
NSError *error = nil;
NSUInteger bytesRead = [representation getBytes:buff fromOffset:0 length:size error:&error];
if (bytesRead && !error) {
NSData *photo = [NSData dataWithBytesNoCopy:buff length:bytesRead freeWhenDone:YES];
CGImageSourceRef cgImage = CGImageSourceCreateWithData((CFDataRef)photo, nil);
NSMutableDictionary *imageMetadata = (NSMutableDictionary *)CGImageSourceCopyPropertiesAtIndex(cgImage, 0, nil);
if (imageMetadata) {
NSLog(@"%@", [imageMetadata description]);
// GPSはこんなかんじで取得
if ([imageMetadata valueForKey:@"{GPS}"]) {
CLLocationDegrees lat = (CLLocationDegrees)[[[imageMetadata objectForKey:@"{GPS}"] objectForKey:@"Latitude"] doubleValue];
CLLocationDegrees lng = (CLLocationDegrees)[[[imageMetadata objectForKey:@"{GPS}"] objectForKey:@"Longitude"] doubleValue];
}
} else {
NSLog(@"no metadata");
}
CFRelease(cgImage);
}
if (error) {
NSLog(@"error:%@", error);
[error release];
free(buff);
}
}
}
failureBlock:^(NSError *error){
NSLog(@"error:%@", error);
}];
}
#import <AssetsLibrary/AssetsLibrary.h>
#import <ImageIO/ImageIO.h>
2、サンプルコード
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary*)info {
[picker dismissModalViewControllerAnimated:NO];
// カメラロールからiPhoneカメラで撮った場合、GPSなど取得可能
NSURL *url = [info objectForKey:UIImagePickerControllerReferenceURL];
ALAssetsLibrary *lib = [[[ALAssetsLibrary alloc] init] autorelease];
NSLog(@">>>>>> %@", url);
[lib assetForURL:url
resultBlock:^(ALAsset *asset){
ALAssetRepresentation *representation = [asset defaultRepresentation];
NSUInteger size = [representation size];
uint8_t *buff = (uint8_t *)malloc(sizeof(uint8_t)*size);
if(buff != nil){
NSError *error = nil;
NSUInteger bytesRead = [representation getBytes:buff fromOffset:0 length:size error:&error];
if (bytesRead && !error) {
NSData *photo = [NSData dataWithBytesNoCopy:buff length:bytesRead freeWhenDone:YES];
CGImageSourceRef cgImage = CGImageSourceCreateWithData((CFDataRef)photo, nil);
NSMutableDictionary *imageMetadata = (NSMutableDictionary *)CGImageSourceCopyPropertiesAtIndex(cgImage, 0, nil);
if (imageMetadata) {
NSLog(@"%@", [imageMetadata description]);
// GPSはこんなかんじで取得
if ([imageMetadata valueForKey:@"{GPS}"]) {
CLLocationDegrees lat = (CLLocationDegrees)[[[imageMetadata objectForKey:@"{GPS}"] objectForKey:@"Latitude"] doubleValue];
CLLocationDegrees lng = (CLLocationDegrees)[[[imageMetadata objectForKey:@"{GPS}"] objectForKey:@"Longitude"] doubleValue];
}
} else {
NSLog(@"no metadata");
}
CFRelease(cgImage);
}
if (error) {
NSLog(@"error:%@", error);
[error release];
free(buff);
}
}
}
failureBlock:^(NSError *error){
NSLog(@"error:%@", error);
}];
}
