ios - 通过 UIImagepickerController 快速获取元数据

标签 ios swift

我正在尝试实现 iOS 8 UIImage Metadata 中的答案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)

}

这会导致“ fatal error :在展开可选值时意外发现 nil”

最佳答案

试试这段代码:

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

关于ios - 通过 UIImagepickerController 快速获取元数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26391158/

相关文章:

objective-c - 快速设置 UIToolBar 背景图片

json - swift ,对象映射器 : Type 'User' does not conform to protocol 'Mappable'

iOS : detecting finger leaving the screen on the sides ?

ios - 如何解释 AudioBuffer 并获得权力?

ios - 快速禁用 UITextField 中的闪烁光标?

ios - 当UITextView的文本超出1行时展开UITextView和UITableView

ios - 如何让 UICollectionViewCell 成为设备的整个宽度?

ios - 具有通用解码类型的网络调用

ios - Swift 中的@dynamicCallable 是什么?

ios - Realm 是否具有相当于 fetchResultsController 的 .NSFetchedResultsChangeMove :