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

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

我已经发现了如何获取原始图像的元数据,并且我能够将这些元数据保存到我创建的最终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的Apple文档:

If the CIImage object is the output of a filter (or filter chain),this method returns the Metadata from the filter’s original input image.

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

您需要自己保存属性.为此,您需要将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,(__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 *,但我这样做只是为了完整性.

相关文章

UITabBarController 是 iOS 中用于管理和显示选项卡界面的一...
UITableView的重用机制避免了频繁创建和销毁单元格的开销,使...
Objective-C中,类的实例变量(instance variables)和属性(...
从内存管理的角度来看,block可以作为方法的传入参数是因为b...
WKWebView 是 iOS 开发中用于显示网页内容的组件,它是在 iO...
OC中常用的多线程编程技术: 1. NSThread NSThread是Objecti...