ios 当从PHAsset上传一个大文件时,我需要复制一个临时文件吗?

2wnc66cl  于 2023-05-02  发布在  iOS
关注(0)|答案(1)|浏览(154)

我试图通过谷歌可恢复上传功能从iOS设备上传文件。我尝试的是PhotoKit和uploadTask(with:fromFile)的方法。

extension ExampleViewController: PHPickerViewControllerDelegate {
    public func picker(_ picker: PHPickerViewController, didFinishPicking results: [PHPickerResult]) {
        ....
        let identifiers = results.compactMap { $0.assetIdentifier }

        identifiers.forEach { identifier in 
            getFileUrlFromPHAsset(assetIdentifier: identifier) { fileUrl in 
                // googleSessionUri is a url to upload a file to
                var request = URLRequest(url: URL(string: googleSessionUriToUpload)!)
                request.method = .put
                let task = urlSession.uploadTask(with: request, fromFile: fileUrl)
                task.resume()
            }
        }
    }
}

func getFileUrlFromPHAsset(assetIdentifier: String, completion: ((URL) -> Void)) {
    guard let asset = PHAsset.fetchAssets(withLocalIdentifiers: [assetIdentifier], options: nil).firstObject else {
        return 
    }
            
    let option = PHVideoRequestOptions()
    option.isNetworkAccessAllowed = true // for icloud files? not sure
    PHImageManager.default().requestAVAsset(forVideo: asset, options: option, resultHandler: { avAsset, _, _ in
        if let urlAsset = avAsset as? AVURLAsset {
            completion(urlAsset.url)
        } else {
            // TODO: error handling..
        }
    })      
}

但我得到了一个错误BackgroundSession <D4B1D24E-B567-4935-8589-40FF99845CD6> Failed to issue sandbox extension for file file:///var/mobile/Media/DCIM/100APPLE/IMG_0030.MOV, errno = [1: Operation not permitted]
我应该把这个PHAsset复制到一个临时文件来上传吗?那么,我如何管理临时文件可能消耗磁盘空间的风险?对于我的情况下,视频文件是相当大的,大致千兆字节的大小。在设备没有足够空间的极端情况下,上传可能无法启动。
先谢谢你了。

wgeznvg7

wgeznvg71#

如果您担心空间不足,请尝试将视频转换为数据,然后使用multipart技术上传。

相关问题