如何在编辑PHAsset时保留原始照片元数据? [英] How to preserve original photo metadata when editing PHAssets?
本文介绍了如何在编辑PHAsset时保留原始照片元数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在通过PhotoKit编辑照片,但我发现这不能保存原始照片的元数据。即使是苹果公司提供的SamplePhotosApp,当他们应用深褐色或Chrome滤镜时,也会发生这种情况。我的问题是,您如何确保保留所有原始照片元数据?
我已经了解了如何获取原始图像的元数据,并且我能够将该元数据保存到我创建的最终CIImage
,但在提交编辑时它仍然被删除。将CIImage
转换为CGImage
到UIImage
到NSData
的方式或将其写入磁盘的方式肯定有问题。
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标签:
编辑照片后:
推荐答案
根据苹果的CIImage.Properties文档:
如果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强制转换为NSD字典*,但我这样做只是为了完整性。
这篇关于如何在编辑PHAsset时保留原始照片元数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文