使用Swift(iOS)和Photos框架保存自定义图像的元数据



我正在尝试将元数据添加到我的合成生成中,并使用照片框架将其保存到相机胶卷中。我已经完成了保存和编辑工作,但我似乎只是想知道如何添加元数据。我尝试过很多方法,比如通过创建CoreGraphics图像来添加元数据(请参阅下面的代码)。所有这些方法都不会给我带来错误,但当我在Mac上打开图像时,我就是看不到元数据。

有人能给我指正确的方向吗?

PHPhotoLibrary.sharedPhotoLibrary().performChanges({
let assets : PHFetchResult = PHAsset.fetchAssetsWithLocalIdentifiers([self.localIdentifier], options: nil);
let asset : PHAsset = assets[0] as! PHAsset;
let changeRequest = PHAssetChangeRequest(forAsset: asset);
changeRequest.location = self.currentLocation;
asset.requestContentEditingInputWithOptions(nil, completionHandler: { (input: PHContentEditingInput?,
    info: [NSObject : AnyObject]) -> Void in

    guard let input = input else { return }
    let imageManager : PHImageManager = PHImageManager();
    let requestoptions : PHImageRequestOptions = PHImageRequestOptions();
    requestoptions.resizeMode = PHImageRequestOptionsResizeMode.None;
    imageManager.requestImageForAsset(asset, targetSize: PHImageManagerMaximumSize, contentMode: PHImageContentMode.Default, options: PHImageRequestOptions(), resultHandler: { (let image : UIImage?, _) -> Void in
        let output : PHContentEditingOutput = PHContentEditingOutput(contentEditingInput: input);
        PHPhotoLibrary.sharedPhotoLibrary().performChanges({ () -> Void in
            let changeRequest = PHAssetChangeRequest(forAsset: asset);
            /* Neu */
            let imageData : NSData = NSData(contentsOfURL: (input.fullSizeImageURL)!)!;
            let image : CIImage = CIImage(data: imageData)!;
            let dataPtr = CFDataCreate(kCFAllocatorDefault, UnsafePointer<UInt8>(imageData.bytes), imageData.length)

            // Save off the properties
            let imageSource : CGImageSourceRef = CGImageSourceCreateWithData(dataPtr, nil)!;
            var metadata : NSMutableDictionary = NSMutableDictionary(dictionary: CGImageSourceCopyProperties(imageSource, nil)!);

/* Add some values to metadata */
....
            NSLog("New metadata: %@", metadata);
            // Save the image
            let outputImageSource : CGImageSourceRef = CGImageSourceCreateWithData(dataPtr, nil)!;
            let jpegData : CFMutableDataRef = CFDataCreateMutable(kCFAllocatorDefault, 0);
            let outputDestination : CGImageDestinationRef = CGImageDestinationCreateWithData(jpegData, CGImageSourceGetType(outputImageSource)!, 1, nil)!;
            // add the image data to the destination
            CGImageDestinationAddImageFromSource(outputDestination, outputImageSource, 0, 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.");
            }

            (jpegData as NSData).writeToURL(output.renderedContentURL, atomically: true);

            let options : String = NSString(format: "%f|%f|%f|%f|%f|%f", self.saturationSlider.value, self.warmthSlider.value, self.brightnessSlider.value, self.sharpnessSlider.value, self.contrastSlider.value, self.gammaSlider.value ) as String;
            let nsObject: AnyObject? = NSBundle.mainBundle().infoDictionary!["CFBundleShortVersionString"];
            output.adjustmentData = PHAdjustmentData(formatIdentifier: NSBundle.mainBundle().bundleIdentifier!,
                formatVersion: nsObject as! String,
                data: options.dataUsingEncoding(NSUTF8StringEncoding)!);
            changeRequest.contentEditingOutput = output;
            }, completionHandler: { (_bool, _error) -> Void in
                if !_bool && error != nil
                {
                    NSLog("%@", error!);
                }
        });
    });
});
}, completionHandler: { (_bool, _error) -> Void in
});

您可以在creationRequest对象上添加/设置一些属性。我不知道如何添加自定义元数据。

PHPhotoLibrary.shared().performChanges({
    let creationRequest = PHAssetChangeRequest.creationRequestForAsset(from: self.anImage!)
    let aLocation = CLLocation(latitude: 27.63866, longitude: -80.39707)
    creationRequest.location = aLocation
    creationRequest.isFavorite = true
    creationRequest.creationDate = Date()
}, completionHandler: {success, error in
    if !success {
        print("error creating asset: (error!)")
    } else {
        print("success creating asset!")
    }
})

最新更新