Save original image data with modified metadata (no re-encoding) on iOS

后端 未结 3 2023
一整个雨季
一整个雨季 2020-12-28 10:07

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 ALAss

相关标签:
3条回答
  • 2020-12-28 10:27

    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.

    0 讨论(0)
  • 2020-12-28 10:35

    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;
    }
    
    0 讨论(0)
  • 2020-12-28 10:39

    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.

    0 讨论(0)
提交回复
热议问题