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

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

概述我正在尝试通过使用Photos框架将合成生成的元数据添加到相机卷中.我得到了保存和编辑工作,但我似乎可以弄清楚如何添加元数据.我尝试了很多方法,比如通过创建CoreGraphics图像来添加元数据(参见下面的代码).所有这些方法都没有给我一个错误,但是当我在Mac上打开图像时,我无法看到元数据. 任何人都能指出我在正确的方向吗? PHPhotoLibrary.sharedPhotoLibrary( 我正在尝试通过使用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!")    }})
总结

以上是内存溢出为你收集整理的使用Swift(iOS)和Photos框架保存自定义图像的元数据全部内容,希望文章能够帮你解决使用Swift(iOS)和Photos框架保存自定义图像的元数据所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1014357.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-22
下一篇 2022-05-22

发表评论

登录后才能评论

评论列表(0条)

保存