微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

我正在尝试通过使用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,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!);
                }
        });
    });
});
},_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!")
    }
})

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐