从ios swift中的图像获取位置详细信息

时间:2017-12-12 06:15:36

标签: swift photo phasset uipickerviewcontroller

我正在尝试从图像中获取位置详细信息 UIImagePickerControllerReferenceURL但我发现PHAsset.fetchAssets(withALAssetURLs: [URL], options: opts)已被弃用。请帮助我获取位置详细信息。

我们可以使用PHAssetCollection吗?如果是的话请帮帮我

   public func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any])
{
    print(info)
    let chosenImage = info[UIImagePickerControllerOriginalImage] as! UIImage
    selectedImage.contentMode = .scaleAspectFit
    selectedImage.image = chosenImage
    dismiss(animated:true, completion: nil)

    if let URL = info[UIImagePickerControllerReferenceURL] as? URL {
        let opts = PHFetchOptions()
        opts.fetchLimit = 1
        let assets = PHAsset.fetchAssets(withALAssetURLs: [URL], options: opts)
        let asset = assets[0]
        print(asset.location)
        // The location is "asset.location", as a CLLocation

        // ... Other stuff like dismiss omitted
    }
}

1 个答案:

答案 0 :(得分:1)

到目前为止,我发现的解决方案是即使在iOS 11中也使用iOS 10代码块,只是忽略UIImagePickerControllerReferenceURL已弃用的消息(该密钥仍然存在且适用于iOS 11)

import AssetsLibrary

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {

    if let imageUrl = info[UIImagePickerControllerReferenceURL] as? NSURL{
        print(imageUrl.absoluteString)  //"assets-library://asset/asset.JPG?id=ED7AC36B-A150-4C38-BB8C-B6D696F4F2ED&ext=JPG"


        // access image from URL
        let assetLibrary = ALAssetsLibrary()
        assetLibrary.asset(for: imageUrl as URL! , resultBlock: { (asset: ALAsset!) -> Void in
            if let actualAsset = asset as ALAsset? {
                let assetRep: ALAssetRepresentation = actualAsset.defaultRepresentation()
                let iref = assetRep.fullResolutionImage().takeUnretainedValue()
                let image = UIImage.init(cgImage: iref)
                self.img.image = image
            }
        }, failureBlock: { (error) -> Void in
        })
    }

    dismiss(animated: true, completion: nil)
}

希望这会有所帮助。