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

Getting the URL Path of an Image from device in iOS Swift

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

现在,当我在模拟器上 运行 我的代码时,我在 info 中得到 9 key/value 对。但是当我 运行 在实际设备上使用相同的代码时,我只得到 3 key/value 对,因此我没有得到 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)
                    })
            }
}

使用 [=h11=] 而不是 asset.requestImagebelowPHAsset 检索图像 URL:

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