Print UIImage metadata to label - ios

I'm attempting to read the metadata produced by a UIImage when shot from the UIImagePicker, and I'm having some trouble.
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
image = [info objectForKey:#"UIImagePickerControllerOriginalImage"];
// get the metadata
NSDictionary *imageMetadata = [info objectForKey:UIImagePickerControllerMediaMetadata];
NSLog (#"imageMetaData %#",imageMetadata);
This successfully prints the metadata dictionary to NSLog.
The issue I'm having is that I would like to access specific indices of the dictionary (such as FNumber, ISO, etc.) and print them to specific labels, but I can't figure out how to access the individual data.
Here is what I have tried so far to pull the data, but it doesn't seem to find the key (it returns as NULL):
NSLog(#"ISO: %#", imageMetadata[#"ISOSpeedRatings"]);
Based off of what NSLog prints for the dictionary, it seems as if there may be dictionaries within the dictionary, and that's what's throwing me off.
Here's what gets printed for the metadata:
imageMetaData {
DPIHeight = 72;
DPIWidth = 72;
Orientation = 6;
"{Exif}" = {
ApertureValue = "2.27500704749987";
BrightnessValue = "-0.6309286396300304";
ColorSpace = 1;
DateTimeDigitized = "2015:04:01 10:33:37";
DateTimeOriginal = "2015:04:01 10:33:37";
ExposureBiasValue = 0;
ExposureMode = 0;
ExposureProgram = 2;
ExposureTime = "0.06666666666666667";
FNumber = "2.2";
Flash = 24;
FocalLenIn35mmFilm = 29;
FocalLength = "4.15";
ISOSpeedRatings = (
320
);
LensMake = Apple;
LensModel = "iPhone 6 back camera 4.15mm f/2.2";
LensSpecification = (
"4.15",
"4.15",
"2.2",
"2.2"
);
MeteringMode = 5;
PixelXDimension = 3264;
PixelYDimension = 2448;
SceneType = 1;
SensingMethod = 2;
ShutterSpeedValue = "3.907056515078773";
SubjectArea = (
1631,
1223,
1795,
1077
);
SubsecTimeDigitized = 705;
SubsecTimeOriginal = 705;
WhiteBalance = 0;
};
"{MakerApple}" = {
1 = 2;
14 = 0;
2 = <0f000b00 06000900 04005000 a900b100 b700bb00 c400cd00 cd00a400 b100c700 14000b00 05000900 06000a00 8a00a800 b000b800 c300cb00 c900cd00 b300a600 2f000700 06000700 0a000400 3500a400 ab00b300 bc00c300 cf00d300 b4007f00 3f000700 09000700 0a000700 05007100 a100af00 b500c200 ce00cd00 a9006b00 1f000a00 0b000900 0a000c00 05001e00 9c00aa00 b400c200 cc00d000 d4005700 2b001900 0d001000 10000d00 08000600 5b00a700 b300bf00 cb00d500 e3008600 eb002800 1a001700 14000c00 0b000700 10009400 b100c000 ce00e000 f400bd00 cf013e00 2a001200 17000f00 0d000800 07004200 b100c000 d300e900 fd000401 ff011101 1d000700 16001400 09000700 07000900 8900bf00 d800ec00 07011f01 10021102 39000b00 10001900 0e000800 0a000700 2c00bf00 dd00f400 0b012401 1e023802 1f010d00 07001900 16000c00 0c000800 21007000 c500f400 0a012e01 10022202 01022500 08001000 18001100 0d001800 1601cc00 d100eb00 09012201 fb011002 26020401 0f000700 16001400 3200e801 6001b000 ce00f400 08011601 e1010602 1a020302 23001700 21002300 84009300 9f00ad00 bf00e800 02011401 ca01fc01 19024002 08013d00 3500ca00 7c009200 9e00ab00 c200d700 f8000a01 b401f101 1b024802 28023000 4a007f00 7f008e00 a000b100 bd00d000 ec00fe00>;
3 = {
epoch = 0;
flags = 1;
timescale = 1000000000;
value = 777291330499583;
};
4 = 1;
5 = 128;
6 = 123;
7 = 1;
8 = (
"0.2226092",
"-0.5721548",
"-0.7796207"
);
9 = 275;
};
"{TIFF}" = {
DateTime = "2015:04:01 10:33:37";
Make = Apple;
Model = "iPhone 6";
Software = "8.1.2";
XResolution = 72;
YResolution = 72;
};
}
Is the data I'm looking for within another NSDictionary named Exif? And if so, how do I access it?

#david strauss can you try this once
- (void) saveImage:(UIImage *)imageToSave withInfo:(NSDictionary *)info{
// Get the assets library
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
// Get the image metadata (EXIF & TIFF)
NSMutableDictionary * imageMetadata = [[info objectForKey:UIImagePickerControllerMediaMetadata] mutableCopy];
// add GPS data
CLLocation * loc = <•••>; // need a location here
if ( loc ) {
[imageMetadata setObject:[self gpsDictionaryForLocation:loc] forKey:(NSString*)kCGImagePropertyGPSDictionary];
}
ALAssetsLibraryWriteImageCompletionBlock imageWriteCompletionBlock =
^(NSURL *newURL, NSError *error) {
if (error) {
NSLog( #"Error writing image with metadata to Photo Library: %#", error );
} else {
NSLog( #"Wrote image %# with metadata %# to Photo Library",newURL,imageMetadata);
}
};
// Save the new image to the Camera Roll
[library writeImageToSavedPhotosAlbum:[imageToSave CGImage]
metadata:imageMetadata
completionBlock:imageWriteCompletionBlock];
[imageMetadata release];
[library release];
}

The metadata dictionary as you've noticed consists of several dictionaries.
So to answer your question - yes, if you're looking for specific values you can access the inner dictionaries. Also, you'd better use the proper keys from ImageIO constants; for example:
NSLog(#"%#", imageMetadata[(NSString*)kCGImagePropertyExifDictionary][(NSString*)kCGImagePropertyExifISOSpeedRatings]);
Or, you can use a key-path:
NSString *keyPath = [NSString stringWithFormat:#"%#.%#",
(NSString*)kCGImagePropertyExifDictionary, (NSString*)kCGImagePropertyExifISOSpeedRatings];
NSLog(#"%#", [imageMetadata valueForKeyPath:keyPath]);

your all required data is within this dictionary only.
you can use json formatter to see the exact location of your data.
NSError *error;
NSData *jsonData = [NSJSONSerialization dataWithJSONObject: imageMetadata
options:NSJSONWritingPrettyPrinted // Pass 0 if you don't care about the readability of the generated string
error:&error];
if (! jsonData) {
NSLog(#"Got an error: %#", error);
} else {
NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
NSLog(#"jsonString: %#", jsonString);
}
after this get the exact location of your data from json

Related

Access Gelocation from UIImage

I can get the location information from photo album on ios, but I would like to access image location (latitude and longitude) when I took a photo.
so this is image finished event
public override void FinishedPickingMedia(UIImagePickerController picker, NSDictionary info)
{
//UIImagePickerController.ReferenceUrl
var originalImage = new NSString("UIImagePickerControllerOriginalImage");
var image = (UIImage)info[originalImage];
foreach (var item in info)
Console.WriteLine(item.Key + " " + item.Value);
image.SaveToPhotosAlbum(delegate(UIImage img, NSError err)
{
Console.WriteLine("Saved!");
});
_dp.DismissModalViewController(true);
OnImageChoosed(image);
}
}
and in info dictionary, i can see those properties
UIImagePickerControllerMediaMetadata {
DPIHeight = 72;
DPIWidth = 72;
Orientation = 6;
"{Exif}" = {
ApertureValue = "2.27500704749987";
BrightnessValue = "5.267792568433492";
ColorSpace = 1;
DateTimeDigitized = "2015:07:08 14:24:15";
DateTimeOriginal = "2015:07:08 14:24:15";
ExposureBiasValue = 0;
ExposureMode = 0;
ExposureProgram = 2;
ExposureTime = "0.008333333333333333";
FNumber = "2.2";
Flash = 24;
FocalLenIn35mmFilm = 29;
FocalLength = "4.15";
ISOSpeedRatings = (
40
);
LensMake = Apple;
LensModel = "iPhone 6 back camera 4.15mm f/2.2";
LensSpecification = (
"4.15",
"4.15",
"2.2",
"2.2"
);
MeteringMode = 5;
PixelXDimension = 3264;
PixelYDimension = 2448;
SceneType = 1;
SensingMethod = 2;
ShutterSpeedValue = "6.908853996279415";
SubjectArea = (
1631,
1223,
1795,
1077
);
SubsecTimeDigitized = 927;
SubsecTimeOriginal = 927;
WhiteBalance = 0;
};
"{MakerApple}" = {
1 = 2;
14 = 0;
2 = <af001d01 9001ed01 9e011f01 70001500 41000d00 0a001000 12000900 0e000e00 b5003201 ae01f701 9b010401 5b001400 47002300 11001500 15000800 0d000d00 c2005501 eb014402 c901d900 67002400 3d002400 0f000b00 08000900 0d000d00 f1009201 30028e02 ff01b200 51000e00 43000e00 0d000e00 0d000d00 0c000b00 2d02b302 f802e902 26029700 80005600 5e000e00 0b000a00 09000900 0a000900 88039703 87033503 3c028300 44001100 45001100 0c000d00 0d000d00 0d000a00 b903b103 97034703 44027700 3e001000 38001000 0d000d00 0e000d00 0d000a00 a4039f03 87031503 22026d00 38000b00 34001000 0d001000 12000b00 0b000900 7b036a03 1d039c02 e2016400 37000e00 32001100 0d000a00 0a000600 07000700 c0029a02 4e02fb01 79015b00 36000e00 31001200 0c000900 0a000700 0c000b00 8b017801 4b013501 01015500 34000a00 30000900 06000c00 0e000800 14001000 b9001a00 34008a00 bd005000 2f000c00 2f000c00 0c000b00 0d000500 07000700 60000e00 0c002400 8f004b00 2f000a00 2d000f00 0e000700 07000700 0b000a00 47000b00 0a000b00 3d003300 28000a00 2b000a00 06000a00 0e000700 12001000 29000a00 09000a00 25003b00 2a000e00 2a000c00 0b000e00 11000700 0a000c00 27000a00 08000a00 0f003f00 3f001b00 31000d00 0e000700 07000700 0a000800>;
3 = {
epoch = 0;
flags = 1;
timescale = 1000000000;
value = 401611073225666;
};
4 = 1;
5 = 128;
6 = 119;
7 = 1;
8 = (
"0.01929908",
"-0.713904",
"-0.7083461"
);
};
"{TIFF}" = {
DateTime = "2015:07:08 14:24:15";
Make = Apple;
Model = "iPhone 6";
Software = "8.3";
XResolution = 72;
YResolution = 72;
};
As you see there is no location information. Well I tried to save image on local after get the location informatin but didnt work.
As I understand, If I can get path of the image which saved to the photo album, I can got
var path = args.Info[UIImagePickerController.ReferenceUrl].ToString();
refeenceURL like above and after I can get the location.. But i dont know how to get path of the newly saved image.
P.S.: I used Xamarin.iOS with C# but doesnt matter if you provide objective C or swift code.
I have an old snippet that I've never used (its an NSMutableDictionary category), because in the app I've used AVFoundation, but it worth a try. Credits to the creator of that code Gustavo Ambrozio.
Since is really old you should adapt for ARC compatibility.
- (id)initWithInfoFromImagePicker:(NSDictionary *)info {
if ((self = [self init])) {
// Key UIImagePickerControllerReferenceURL only exists in iOS 4.1
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 4.1f) {
NSURL* assetURL = nil;
if ((assetURL = [info objectForKey:UIImagePickerControllerReferenceURL])) {
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
[library assetForURL:assetURL
resultBlock:^(ALAsset *asset) {
NSDictionary *metadata = asset.defaultRepresentation.metadata;
[self addEntriesFromDictionary:metadata];
}
failureBlock:^(NSError *error) {
}];
[library autorelease];
}
else {
NSDictionary *metadata = [info objectForKey:UIImagePickerControllerMediaMetadata];
if (metadata)
[self addEntriesFromDictionary:metadata];
}
}
}
return self;
}

flickr.photoset.getlist json parsing in ios

I want to parse a array of json dictionary which is returned to me using the method flickr.photosets.getlist.I am using objectiveFlickr api.
This is my json
"_text" = "\n\n";
photosets = {
"_text" = "\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\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n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n";
cancreate = 1;
page = 1;
pages = 1;
perpage = 871;
photoset = (
{
"_text" = "\n\t\t\n\t\t\n\t";
"can_comment" = 1;
"count_comments" = 0;
"count_views" = 0;
"date_create" = 1403859842;
"date_update" = 0;
description = {
"_text" = "Uploaded from my iPhone/iPod Touch";
};
farm = 6;
id = 72157645380087391;
"needs_interstitial" = 0;
photos = 1;
primary = 14514940751;
secret = ebc107d841;
server = 5237;
title = {
"_text" = banjara;
};
videos = 0;
"visibility_can_see_set" = 1;
},
{
"_text" = "\n\t\t\n\t\t\n\t";
"can_comment" = 1;
"count_comments" = 0;
"count_views" = 0;
"date_create" = 1403849913;
"date_update" = 0;
description = {
"_text" = "Uploaded from my iPhone/iPod Touch";
};
farm = 4;
id = 72157645380777662;
"needs_interstitial" = 0;
photos = 1;
primary = 14516264624;
secret = 70097bb15c;
server = 3867;
title = {
"_text" = iuggjkd;
};
videos = 0;
"visibility_can_see_set" = 1;
}
);
};
I am stuck in this for hours.Can u suggest me how to deal with this.
Thanks for the reply
Try doing something like this. This is not the exact solution and you'll have to work around your way.
NSString *link = [NSString stringWithFormat:#"yourServiceURL"];
NSString *encdLink = [link stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *url=[NSURL URLWithString:encdLink];
NSData *response = [NSData dataWithContentsOfURL:url];
NSError *error;
NSDictionary *json = [NSJSONSerialization JSONObjectWithData:response options: NSJSONReadingMutableContainers error: &error];
Now you have your data in the array and extract it out using objectForKey or valueForKey.You can use NSDictionary or NSArray as per your requirement.
This is what I am doing to fetch images from JSON and showing in my app. Firstly get the array from the above code. Then extract out the image content like this :
_demoArray = [json valueForKey:#"yourKeyField"];
Now you have the values in your demoArray.
Hope this helps.

Not getting Updated TIFF Data of UIImage using ImageIO Framework

I am using ImageIO framework for updating metadata of UIImage then after I saved that image in my iphone photoLibrary my issue is that I am updating the Image TIFF data successfully in image but when I select that image for get TIFF data then it not displaying the TIFF dictionary data .online I can see updated TIFF data but in my code i can't see it.
Updated TIFF Data:
"{TIFF}" = {
Make = "test ";
Model = test;
Software = test;
};
Get updated Image metadata in iOS:
{
ColorModel = RGB;
Depth = 8;
PixelHeight = 2592;
PixelWidth = 1936;
"{PNG}" = {
InterlaceType = 0;
};
}
Code:
NSData *imageData = UIImagePNGRepresentation(image);
CGImageSourceRef imageSource = CGImageSourceCreateWithData((__bridge CFDataRef )imageData, NULL);
if (imageSource == NULL) {
// Error loading image
return nil;
}
//Set the object in dictinary
NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithBool:NO], (NSString *)kCGImageSourceShouldCache,
nil];
CFDictionaryRef imageProperties = CGImageSourceCopyPropertiesAtIndex(imageSource, 0, (__bridge CFDictionaryRef)options);
//Device name
CFDictionaryRef refDeviceInfo = CFDictionaryGetValue(imageProperties, kCGImagePropertyTIFFDictionary);
if (refDeviceInfo) {
NSString *strMake = (NSString *)CFDictionaryGetValue(refDeviceInfo, kCGImagePropertyTIFFMake);
NSLog(#"Make: %#", strMake);
NSString *strModel = (NSString *)CFDictionaryGetValue(refDeviceInfo, kCGImagePropertyTIFFModel);
NSLog(#"Model: %#", strModel);
}

How to save modified UIImage with original meta data?

In my iOS app, I have to make some modification on a UIImage, for example adding a watermark on it. After that, I want to save the image with original meta data, but if I tried to using writeImageDataToSavedPhotosAlbum:metadata:completionBlock:, I got nothing in the produced image.
Since I have to modify the image, so I have to pass the NSData for metadata: part using the following method: UIImageJPEGRepresentation(UIImage *, float).
And right before saving the image, the meta data is like:
Meta: {
ApertureValue = "2.526068811667587";
BrightnessValue = "-1.807443054797895";
ExposureMode = 0;
ExposureProgram = 2;
ExposureTime = "0.05882352941176471";
FNumber = "2.4";
Flash = 16;
FocalLenIn35mmFilm = 33;
FocalLength = "4.12";
ISOSpeedRatings = (
800
);
LensMake = Apple;
LensModel = "iPhone 5c back camera 4.12mm f/2.4";
LensSpecification = (
"4.12",
"4.12",
"2.4",
"2.4"
);
MeteringMode = 3;
PixelXDimension = 3264;
PixelYDimension = 2448;
SceneType = 1;
SensingMethod = 2;
ShutterSpeedValue = "4.058917734171294";
WhiteBalance = 0;
"{GPS}" = {
Altitude = "147.9932";
DOP = "76.42908";
Latitude = "45.7398";
LatitudeRef = N;
Longitude = "126.6266";
LongitudeRef = E;
TimeStamp = "2013:12:23 08:45:30";
};
So, what is wrong here?
Try this,
UIImage *newImage = self.yourOriginalImage;
UIImage *anotherImage = #"waterMark.png";
CGImageRef imageRef = anotherImage.CGImage;
CGImageRef mask = CGImageMaskCreate(CGImageGetWidth(imageRef),
CGImageGetHeight(imageRef),
CGImageGetBitsPerComponent(imageRef),
CGImageGetBitsPerPixel(imageRef),
CGImageGetBytesPerRow(imageRef),
CGImageGetDataProvider(imageRef), NULL, false);
CGImageRef maskedImage = CGImageCreateWithMask([newImage CGImage], mask);
self.imageView.image = [UIImage imageWithCGImage: maskedImage];
Assuming you have a UIImage called myEditedImage and the metadata in an NSDictionary (or an NSMutableDictionary...) called "metadata":
ALAssetsLibrary *library;
ALAssetsLibraryWriteImageCompletionBlock completionBlock = ^(NSURL *assetURL, NSError *error) {};
float compressionLevel = 0.75; // Or whatever you want
NSData *dataIn = UIImageJPEGRepresentation(myEditedImage, compressionLevel);
[library writeImageDataToSavedPhotosAlbum: dataIn
metadata: metadata
completionBlock:completionBlock];

Failure to save image with custom Metadata

I'm trying to create a custom image gallery within my iOS app. I would like to enable the user to be able to save certain meta data with the image so that it can be pulled up in the app later with the attached information.
First, when the user takes a picture, the app saves the image into a custom album for the app:
UITextField *nameField = [alertView textFieldAtIndex:0];
NSMutableDictionary *metaData = [[NSMutableDictionary alloc] init];
[metaData setObject:currentEvent forKey:kMetaDataEventKey];
[metaData setObject:[AppDelegate getActivePerson].name forKey:kMetaDataPersonKey];
[metaData setObject:nameField.text forKey:kMetaDataNameKey];
NSLog(#"Saving image with metadata: %#", metaData);
NSMutableDictionary *realMetaData = [[NSMutableDictionary alloc] init];
[realMetaData setObject:metaData forKey:kCGImagePropertyTIFFDictionary];
[library saveImage:imageToSave toAlbum:albumName metadata:realMetaData withCompletionBlock:^(NSError *error) {
if ( error != nil )
{
NSLog(#"Error saving picture? %#", error);
}
[self.tableView reloadData];
}];
Upon saving I get the following log message:
Saving image with metadata: {
Event = t;
Person = "George James";
PictureName = tt;
}
Then when I attempt to retrieve the images later, I use this function
-(void) loadAssets
{
self.assets = [NSMutableArray arrayWithCapacity:album.numberOfAssets];
[album enumerateAssetsUsingBlock:^(ALAsset *result, NSUInteger index, BOOL *stop) {
if ( result != nil )
{
NSDictionary *metaData = result.defaultRepresentation.metadata;
NSLog(#"Retrieved image metadata: %#", metaData);
}
else
{
[self.tableView reloadData];
}
}];
}
But the log indicates that it did not successfully save the meta data associated with the image:
Retrieved image metadata: {
ColorModel = RGB;
DPIHeight = 72;
DPIWidth = 72;
Depth = 8;
Orientation = 1;
PixelHeight = 720;
PixelWidth = 960;
"{Exif}" = {
ColorSpace = 1;
ComponentsConfiguration = (
1,
2,
3,
0
);
ExifVersion = (
2,
2,
1
);
FlashPixVersion = (
1,
0
);
PixelXDimension = 960;
PixelYDimension = 720;
SceneCaptureType = 0;
};
"{TIFF}" = {
Orientation = 1;
ResolutionUnit = 2;
XResolution = 72;
YResolution = 72;
"_YCbCrPositioning" = 1;
};
}
library is an ALAssetsLibrary instance, and the saveImage: toAlbum: method is from this blog post, only slightly modified so that I can save metadata as such:
-(void)saveImage:(UIImage *)image toAlbum:(NSString *)albumName metadata:(NSDictionary *)metadata withCompletionBlock:(SaveImageCompletion)completionBlock
{
//write the image data to the assets library (camera roll)
[self writeImageToSavedPhotosAlbum:image.CGImage
metadata:metadata
completionBlock:^(NSURL* assetURL, NSError* error) {
//error handling
if (error!=nil) {
completionBlock(error);
return;
}
//add the asset to the custom photo album
[self addAssetURL: assetURL
toAlbum:albumName
withCompletionBlock:completionBlock];
}
];
}
The image is coming from a UIImagePickerController that uses the camera. The picture is successfully being saved to the correct album, just missing the metadata.
Am I doing something wrong in the save/load process? Am I actually not allowed to save custom meta data to an image?
I did some testing, and from what I can tell, the short answer is 'you can't do that.' It looks like the metadata has to conform to specific EXIF Metadata keys. You could look up the available TIFF Metadata keys and see if there are any values you want to set/overwrite. You could try, for example, using kCGImagePropertyTIFFImageDescription to store your data.
NSMutableDictionary *tiffDictionary= [[NSMutableDictionary alloc] init];
NSMutableDictionary *myMetadata = [[NSMutableDictionary alloc] init];
[tiffDictionary setObject:#"My Metadata" forKey:(NSString*)kCGImagePropertyTIFFImageDescription];
[myMetadata setObject:tiffDictionary forKey:(NSString*)kCGImagePropertyTIFFDictionary];
... and save myMetadata with the image.
For other keys, see this:
http://developer.apple.com/library/mac/#documentation/GraphicsImaging/Reference/CGImageProperties_Reference/Reference/reference.html
Otherwise, what I would do is create an NSDictionary that uses an image's unique identifier as a key, and store the metadata object as the value. Save/Load this NSDictionary whenever you save/load an image.

Resources