从iOS Swift中的设备获取图像的URL路径

时间:2019-04-22 05:30:23

标签: ios swift url phasset phimagemanager

我正在尝试在实际设备而非模拟器上使用PHImageManager获取图像路径。 我正在使用photoUrl = info!["PHImageFileURLKey"] as? URL来获取网址。

现在,当我在模拟器上运行代码时,我的信息中得到9 key/value pairs。但是,当我在实际设备中运行相同的代码时,我只有3 key/value pairs,因此我没有得到photoUrl并且我的应用程序崩溃了

这是我一直在尝试实现的代码,


var selectedPhotos : [URL] = []

 func pickerViewController(_ pickerViewController: TatsiPickerViewController, didPickAssets assets: [PHAsset])
    {

            self.selectedPhotos.removeAll()
            let imageManager = PHImageManager.default()
            let options = PHImageRequestOptions()
            options.deliveryMode = .highQualityFormat
            options.resizeMode = .exact
            options.isSynchronous = true

            let count = assets.count
            for asset : PHAsset in assets
            {
                let imageSize = CGSize(width: asset.pixelWidth,
                                       height: asset.pixelHeight)
                /* For faster performance, and maybe degraded image */
                imageManager.requestImage(for: asset,
                                          targetSize: imageSize,
                                          contentMode: .aspectFill,
                                          options: options,
                                          resultHandler:
                    {
                                            (image, info) -> Void in
                         var photoUrl : URL!
                         photoUrl = info!["PHImageFileURLKey"] as? URL
                         self.selectedPhotos.append(photoUrl)
                    })
            }
}

1 个答案:

答案 0 :(得分:0)

使用插入了asset.requestImagebelow的{​​{1}}从URL检索图像PHAsset

func pickerViewController(_ pickerViewController: TatsiPickerViewController, didPickAssets assets: [PHAsset]){

    let count = assets.count
    for asset : PHAsset in assets{            
        asset.requestContentEditingInput(with: PHContentEditingInputRequestOptions()) { (eidtingInput, info) in
            if let input = eidtingInput, let photoUrl = input.fullSizeImageURL {
                self.selectedPhotos.append(photoUrl)
            }
        }
    }
}