如何在编辑PHAssets时保留原始照片元数据?

时间:2014-09-07 22:18:19

标签: ios metadata ios8 photos ciimage

我正在通过PhotoKit编辑照片,但我发现这并不能保留原始照片的元数据。即使Apple应用Sepia或Chrome过滤器时提供的SamplePhotosApp也会出现这种情况。我的问题是,您如何确保保留所有原始照片元数据?

我已经发现了如何获取原始图片的元数据,并且我能够将该元数据保存到我创建的最终CIImage,但在编辑时它仍然被删除承诺。我将CIImage转换为CGImageUIImageNSData的方式一定存在问题,或者我如何将其转换为磁盘。< / p>

asset.requestContentEditingInputWithOptions(options) { (input: PHContentEditingInput!, _) -> Void in
    //Get full image
    let url = input.fullSizeImageURL
    let orientation = self.input.fullSizeImageOrientation
    var inputImage = CIImage(contentsOfURL: url)
    inputImage = inputImage.imageByApplyingOrientation(orientation)

    //do some processing on original photo here and create a CGImage...

    //save the original photo's metadata to a new CIImage:
    let originalMetadata = inputImage.properties()
    let newImage = CIImage(CGImage: editedCGImage, options: [kCIImageProperties: originalMetadata])

    println(newImage.properties()) //correctly prints all metadata!

    //commit changes to disk - somewhere after this line the metadata is lost
    let eaglContext = EAGLContext(API: .OpenGLES2)
    let ciContext = CIContext(EAGLContext: eaglContext)
    let outputImageRef = ciContext.createCGImage(newImage, fromRect: newImage.extent())
    let uiImage = UIImage(CGImage: outputImageRef, scale: 1.0, orientation: UIImageOrientation.Up)
    let jpegNSData = UIImageJPEGRepresentation(uiImage, 0.75)

    let contentEditingOutput = PHContentEditingOutput(contentEditingInput: input)
    let success = jpegData.writeToURL(contentEditingOutput.renderedContentURL, options: NSDataWritingOptions.AtomicWrite, error: _)

    PHPhotoLibrary.sharedPhotoLibrary().performChanges({ () -> Void in
        let request = PHAssetChangeRequest(forAsset: asset)
        request.contentEditingOutput = contentEditingOutput
    }, completionHandler: { (success: Bool, error: NSError!) -> Void in
        if success == false { println('failed to commit image edit: \(error)') }
    })
})

原始 - 请注意GPS标签:
Original photo's metadata

编辑照片后:
enter image description here

2 个答案:

答案 0 :(得分:5)

根据CIImage.properties的Apple文档:

  

如果CIImage对象是过滤器(或过滤器链)的输出,则此方法返回过滤器原始输入图像中的元数据。

话虽如此,如果文档不正确,我就会这样做。

您需要自己保存属性。为此,您需要将URL的内容作为NSData

读取
NSURL *url = @"http://somewebsite.com/path/to/some/image.jpg";
NSData *imageData = [NSData dataWithContentsOfURL:url];
CIImage *image = [CIImage imageWithData:imageData];

// Save off the properties
CGImageSourceRef imageSource = CGImageSourceCreateWithData((__bridge CFDataRef) imageData);
NSDictionary* metadata = (__bridge NSDictionary *) CGImageSourceCopyProperties(imageSource, NULL);

// process your image
// ...
NSData *outputData = // create data from altered image.

// Save the image
CGImageSourceRef outputImageSource = CGImageSourceCreateWithData((__bridge CFDataRef) outputData);
CFMutableDataRef jpegData = CFDataCreateMutable(NULL, 0);
CGImageDestinationRef outputDestination = CGImageDestinationCreateWithData(jpegData, CGImageSourceGetType(outputImageSource), 1, NULL);

// add the image data to the destination
CGImageDestinationAddImageFromSource(jpegData, outputImageSource, 0, (__bridge CFDictionaryRef) metadata);

if (CGImageDestinationFinalize(outputDestination)) {
    NSLog(@"Successful image creation.");
    // process the image rendering, adjustment data creation and finalize the asset edit.
} else {
    NSLog(@"Image creation failed.");
}

如果您不需要修改元数据,则无需将CFDataRef强制转换为NSDictionary *,但我这样做是为了完整性。

答案 1 :(得分:4)

虽然这可以像Bradley所展示的那样在Objective-C中完成,但它在Swift中无法正常工作并且会导致performChanges块失败(请参阅我的other question)。这已在最新版本中解决 - Xcode 6.3中的Swift 1.2。为了保留元数据,这很简单:

let metadata = CGImageSourceCopyPropertiesAtIndex(imageSource, 0, nil) as NSDictionary
CGImageDestinationAddImage(destination, cgImage, metadata)

或者对于Objective-C:

NSMutableDictionary *imageMetadata = [(NSDictionary *) CFBridgingRelease(CGImageSourceCopyPropertiesAtIndex(imageSource, 0, NULL)) mutableCopy];
CGImageDestinationAddImageFromSource(destination, imageSource, 0, (__bridge CFDictionaryRef)(imageMetadata));

编辑:虽然这在iOS模拟器中有效,但它无法在运行Swift代码的真实设备上运行。