小编典典

通过UIImagepickerController快速获取元数据

swift

我正在尝试在iOS 8中针对SWIFT中的Objective-C 实现答案

//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)

}

这将导致“致命错误:在展开可选值时意外发现nil”


阅读 304

收藏
2020-07-07

共1个答案

小编典典

试试这个代码:

let metadata = info[UIImagePickerControllerMediaMetadata] as? NSDictionary    
let image = info[UIImagePickerControllerOriginalImage] as? UIImage
2020-07-07