gpt4 book ai didi

ios - didFinishPickingMediaWithInfo 在 iOS 13 中返回不同的 URL

转载 作者:行者123 更新时间:2023-12-01 22:58:54 27 4
gpt4 key购买 nike

- (void)videoPickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<UIImagePickerControllerInfoKey,id> *)info

在 iOS 13 和其他 iOS 中返回不同的 URL。

知道为什么会发生这种情况吗?

iOS 13:

file:///private/var/mobile/Containers/Data/PluginKitPlugin/0849234B-837C-43ED-BEDD-DE4F79E7CE96/tmp/trim.B8AB021D-F4B6-4E50-A93C-8B7F7FB40A1C.MOV

file:///private/var/mobile/Containers/Data/Application/5AE52A95-6A2F-49A5-8210-D70E022E9A05/tmp/5A8D81B5-FC42-4228-9514-CD998A4E7FA9.MOV

这导致我出现错误,因为我没有 PluginKitPlugin 的权限。文件夹。

在这两种情况下,我都使用 imagePicker 选择视频.

最佳答案

我为此苦苦挣扎了几个晚上,终于解决了这个问题。

此处用例的差异之一是我将视频上传到 AWS S3。这是通过后台线程中的 S3 传输实用程序发生的。经过大量的实验和调试,这就是我的决定。

变化在于,在 iOS 13 中,图像选择器 Controller didFinishPickingMediaWithInfo 方法的 info[.mediaURL] 参数中返回的 mediaURL 指向“PluginKitsPlugin ”目录。我们的应用似乎很长时间都无法访问该位置。

示例:file:///private/var/mobile/Containers/Data/PluginKitPlugin/0849234B-837C-43ED-BEDD-DE4F79E7CE96/tmp/trim。 B8AB021D-F4B6-4E50-A93C-8B7F7FB40A1C.MOV

由于某种原因(也许其他人知道)只能暂时访问该 URL。这里的一些理论表明,关闭图像选择器 Controller 将取消分配 URL,从而使其无效。

根据这个理论,我尝试通过两种不同的方式解决这个问题:

  1. 在上传完成之前,请勿关闭图像选择器。这不起作用。 S3 传输实用程序的后台进程仍然因“找不到文件”错误而悄然死亡。
  2. 传递对信息字典的引用,并在尽可能靠近上传点的地方使用它。我正在上传到 AWS S3,因此当 S3 在后台上传时,信息字典的取消引用可能仍在发生。

最终解决问题的方法是将信息[.mediaURL]复制到我的应用程序临时文件夹中的另一个可用位置。

这是我用来将信息[.mediaURL]复制到我的应用程序临时文件夹的代码。

     This function will copy a video file to a temporary location so that it remains accessbile for further handling such as an upload to S3.
- Parameter url: This is the url of the media item.
- Returns: Return a new URL for the local copy of the vidoe file.
*/
func createTemporaryURLforVideoFile(url: NSURL) -> NSURL {
/// Create the temporary directory.
let temporaryDirectoryURL = URL(fileURLWithPath: NSTemporaryDirectory(), isDirectory: true)
/// create a temporary file for us to copy the video to.
let temporaryFileURL = temporaryDirectoryURL.appendingPathComponent(url.lastPathComponent ?? "")
/// Attempt the copy.
do {
try FileManager().copyItem(at: url.absoluteURL!, to: temporaryFileURL)
} catch {
print("There was an error copying the video file to the temporary location.")
}

return temporaryFileURL as NSURL
}

此代码将文件复制到临时目录(如下所示),您的应用程序在其生命周期内可以访问该临时目录:文件:///private/var/mobile/Containers/Data/Application/5AE52A95-6A2F-49A5-8210-D70E022E9A05/tmp/5A8D81B5-FC42-4228-9514-CD998A4E7FA9.MOV

您会注意到,选择要上传的图像 (info[.imageURL]) 会在同一目录中返回一个文件。上传图像之前没有出现任何问题。

这样,S3 传输实用程序就能够在后台线程中访问该文件并完成将视频上传到 S3。

关于ios - didFinishPickingMediaWithInfo 在 iOS 13 中返回不同的 URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57798968/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com