12

I want to save an image with some metadata changes in a temp folder, without re-encoding the actual image data.

The only method that I found able to do this is ALAssetsLibrary/writeImageDataToSavedPhotosAlbum:metadata:completionBlock:, however, this one saves the image to the Photo Library. Instead, I want to save the image to a temp folder (for instance to share it by email, without populating the Photo Library).

I've tried using CGImageDestinationRef (CGImageDestinationAddImageFromSource), but it can only be created using a decoded image, which means it's re-encoding it when saved (tested, pixel bytes look different).

Are there any other methods/classes available for iOS that can save image data along with metadata, besides using CGImageDestinationRef? Suggestions for workarounds would also be welcomed.

alex-i
  • 5,406
  • 2
  • 36
  • 56
  • Can you elaborate a little - where are you getting the images from (camera? other?) – foundry Jan 31 '13 at 21:55
  • It doesn't really matter, I have them as undecoded `NSData` (usually read from ALAsset). – alex-i Feb 01 '13 at 06:37
  • Considering you have read from some source, its is already encoded, updated the meta, how does it fail if you just write it to a file? You know, `[data writeToFile: atomically:]`. Just wondering. – Nandeep Mali Feb 02 '13 at 11:33
  • @NandeepMali If I write it directly to file I can't change the metadata. I basically have 1 `NSData` (undecoded image) object and 1 `NSDictionary` (metadata) object that I somehow need to merge together without altering the image pixels. – alex-i Feb 02 '13 at 11:45
  • Ah I see. Do you have a specific image format that you are working with? – Nandeep Mali Feb 02 '13 at 11:46
  • @NandeepMali usually jpeg, but sometimes png as well (depends what the user has in his Photo Library). I usually make changes (if the image supports it) to kCGImagePropertyTIFFDictionary, kCGImagePropertyJFIFDictionary, kCGImagePropertyExifDictionary, kCGImagePropertyPNGDictionary, kCGImagePropertyIPTCDictionary, kCGImagePropertyGPSDictionary, kCGImagePropertyExifAuxDictionary from https://developer.apple.com/library/mac/#documentation/graphicsimaging/reference/CGImageProperties_Reference/Reference/reference.html – alex-i Feb 02 '13 at 12:32

3 Answers3

12

This is a frustrating problem with the iOS SDK. First, I recommend filing an enhancement request.

Now, here is a potential workaround: IF the ALAsset was created by you (i.e., its editable property is YES) then you can basically read the data, write with metadata, read again, save to disk, and then write with the original metadata.

This approach will avoid creating a duplicate image.

Please read the // comments as I skipped some stuff for brevity (like building a metadata dictionary):

ALAsset* asset; //get your asset, don't use this empty one
if (asset.editable) {

    // get the source data
    ALAssetRepresentation *rep = [asset defaultRepresentation];
    Byte *buffer = (Byte*)malloc(rep.size);
    // add error checking here
    NSUInteger buffered = [rep getBytes:buffer fromOffset:0.0 length:rep.size error:nil];
    NSData *sourceData = [NSData dataWithBytesNoCopy:buffer length:buffered freeWhenDone:YES];

    // make your metadata whatever you want
    // you should use actual metadata, not a blank dictionary
    NSDictionary *metadataDictionary = [NSDictionary dictionary];

    // these are __weak to avoid creating an ARC retain cycle
    NSData __weak *originalData = sourceData;
    NSDictionary __weak *originalMetadata = [rep metadata];

    [asset setImageData:sourceData
               metadata:metadataDictionary
        completionBlock:^(NSURL *assetURL, NSError *error) {
            //now get your data and write it to file
            if (!error) {
                //get your data...
                NSString *assetPath = [assetURL path];
                NSData *targetData = [[NSFileManager defaultManager] contentsAtPath:assetPath];

                //...write to file...
                NSArray *searchPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
                NSString *documentPath = [searchPaths lastObject];
                NSURL *fileURL = [NSURL fileURLWithPath:documentPath];
                [targetData writeToURL:fileURL atomically:YES];

                //...and put it back the way it was
                [asset setImageData:originalData metadata:originalMetadata completionBlock:nil];
            } else {
                // handle error on setting data
                NSLog(@"ERROR: %@", [error localizedDescription]);
            }
        }];
} else {
    // you'll need to make a new ALAsset which you have permission to edit and then try again

}

As you can see, if the ALAsset isn't owned by you, you will need to create one, which will add a photo to the user's library, which is exactly what you wanted to avoid. However, as you may have guessed, you cannot delete an ALAsset from the user's photo library, even if your app created it. (Feel free to file another enhancement request for that.)

So, if the photo/image was created in your app, this will work for you.

But if not, it will create an additional copy the user must delete.

The only alternative is to parse the NSData yourself, which would be a pain. There is no open-source library that I am aware of to fill this gap in the iOS SDK.

Aaron Brager
  • 65,323
  • 19
  • 161
  • 287
  • Thanks. I found one opensource library, but that wasn't updated in a while, and only supports some of the metadata. – alex-i Feb 06 '13 at 11:01
  • Here's the link for it: http://code.google.com/p/iphone-exif/ . I only had a look over its description so far, since it seems to only be working for exif. – alex-i Feb 06 '13 at 13:11
  • @AaronBrager I am getting assetURL = nil, although asset is editable. Any idea of why can this be happening? – Matias Apr 04 '16 at 16:10
  • @Matias From the docs: *"If the image is not saved, `assetURL` is `nil`."* Inspect `error` to see why. – Aaron Brager Apr 04 '16 at 17:56
  • @AaronBrager The weird thing is that error is nil also. – Matias Apr 05 '16 at 14:46
2

Try This :

- (void)saveImage: (UIImage*)image
{
    if (image != nil)
    {
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
                                                             NSUserDomainMask, YES);
        NSString *documentsDirectory = [paths objectAtIndex:0];
        NSString* path = [documentsDirectory stringByAppendingPathComponent:
                          @"test.png" ];
        NSData* data = UIImagePNGRepresentation(image);
        [data writeToFile:path atomically:YES];
    }
}

- (UIImage*)loadImage
{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
                                                         NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString* path = [documentsDirectory stringByAppendingPathComponent:
                      @"test.png" ];
    UIImage* image = [UIImage imageWithContentsOfFile:path];
    [self sendAction:path];
    return image;
}
Meet Doshi
  • 4,241
  • 10
  • 40
  • 81
Mutawe
  • 6,464
  • 3
  • 47
  • 90
  • Thanks, but this would re-encode the images, unless they were initially png in which case I'm not sure. – alex-i Feb 06 '13 at 09:11
2

Alongside iphone-exif, and Aaron's answer, you might also want to look at the libexif c library.

see also HCO23's answer to
How to write or modify EXIF data for an existing image on the filesystem, without loading the image?

(@HCO23 has used libexif in iOS projects).

It's also worth noting that many of the 'professional' metadata editing apps in the app store seem to skirt around the issue by creating sidecar/xmp files.

Community
  • 1
  • 1
foundry
  • 31,615
  • 9
  • 90
  • 125