通过UIImagepickerController在swift中获取元数据
我试图在SWIFT中的Objective-C的iOS 8 UIImage元数据中实现如下的答案
//source: http://stackanswers.com/questions/24227578/ios-8-uiimage-metadata func metaDataFromAssetLibrary(info: NSDictionary) { var assetURL = info.objectForKey(UIImagePickerControllerReferenceURL) as NSURL var assetLibrary = ALAssetsLibrary() assetLibrary.assetForURL(assetURL as NSURL, resultBlock: { (asset: ALAsset!) in var metadata: NSDictionary = asset.defaultRepresentation().metadata() as NSDictionary NSLog ("imageMetaData from AssetLibrary %@",metadata); }, failureBlock: { (error: NSError!) in NSLog("Error!") }) }
我打电话给它
func imagePickerController(picker: UIImagePickerController!, didFinishPickingMediaWithInfo info: NSDictionary!){ // https://stackoverflow.com/questions/3088874/didfinishpickingmediawithinfo-return-nil-photo picker.dismissViewControllerAnimated(true, completion: nil) let image = info.objectForKey("UIImagePickerControllerOriginalImage") as UIImage PHPhotoLibrary.sharedPhotoLibrary().performChanges({ let createAssetRequest = PHAssetChangeRequest.creationRequestForAssetFromImage(image) let assetPlaceholder = createAssetRequest.placeholderForCreatedAsset let albumChangeRequest = PHAssetCollectionChangeRequest(forAssetCollection: self.assetCollection, assets: self.photosAsset) albumChangeRequest.addAssets([assetPlaceholder]) }, completionHandler: {(success, error)in NSLog("Adding Image to Library -> %@", (success ? "Sucess":"Error!")) picker.dismissViewControllerAnimated(true, completion: nil) }) metaDataFromAssetLibrary(info) }
导致一个“致命的错误:意外地发现零,而展开一个可选值”
试试这个代码:
let metadata = info[UIImagePickerControllerMediaMetadata] as? NSDictionary let image = info[UIImagePickerControllerOriginalImage] as? UIImage
如果你从相机获取图像,你可以从信息[UIImagePickerControllerMediaMetadata]获取元数据。
但是,当你从相册中select图像,没有信息[UIImagePickerControllerMediaMetadata],但信息[UIImagePickerControllerReferenceURL]!
- 从Swift类访问Objective-c基类的实例variables
- 使用用户名而不是电子邮件进入ios应用程序和firebase
- 如何打开文件并在其中附加一个string,swift
- XCode 8奇怪的游乐场错误,无法查找符号
- 使用Swift从您的应用程序发送消息到WhatsApp?
- signInSilently()生成一个错误代码= -4
- Swift AVCaptureSessionclosures打开button错误:当前不支持多个audio/videoAVCaptureInputs
- 如何在Core Data上下文之外使用Core Data模型子类?
- 获取UIPageViewController的滚动位置