Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
702 views
in Technique[技术] by (71.8m points)

ios - Getting metadata in swift by UIImagepickerController

I'm trying to implement the answer in iOS 8 UIImage Metadata for Objective-C in SWIFT as follows

//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!")
    })

}

I'm calling it with

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)

}

which results in a "fatal error: unexpectedly found nil while unwrapping an Optional value"

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Try this code:

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...