ios - 应用程序崩溃, "Lost Connection"

标签 ios swift xcode avassetexportsession

类似于此this question ,

我遇到了问题,我的应用程序以同样的方式崩溃。我会假设与另一个问题相同的答案:内存问题;除了我在 AVAssetExportSession 调用期间发生崩溃。

guard let exporter = AVAssetExportSession(asset: mixComposition, presetName: AVAssetExportPresetHighestQuality) else { return }
        exporter.outputFileType = AVFileTypeMPEG4
        exporter.outputURL = url
        exporter.videoComposition = mainComposition
        print("done")

        exporter.exportAsynchronously(completionHandler: {
            DispatchQueue.main.async(execute: {
                self.exportDidFinish(exporter)
                print("removing AI")
                self.removeAI()
                print("removed AI")
                completion()
            })
        })

func exportDidFinish(_ exporter:AVAssetExportSession) {
    if(exporter.status == AVAssetExportSessionStatus.completed) {
        print("cool")
    }
    else if(exporter.status == AVAssetExportSessionStatus.failed) {
        print(exporter.error as Any)
    }
}

它打印“完成”但从不打印“删除 AI”。它也不会打印“酷”或“(错误)”;它崩溃并在 XCode 的顶部说“失去与 iPhone 的连接......”正如其他问题所述。

我认为这是一个内存问题,但在异步导出期间(据我所知)之间没有发生任何事情,因为我只是在等待调用完成处理程序。但是两者之间没有任何调用,我不确定如何处理。有什么想法吗?

最佳答案

我认为 AVAssetExportSession 对象可能会在运行异步任务时被释放。考虑将它作为一个类变量来确保异步 block 可以完成它的任务。它将是这样的:

class myClass {
    var exporter: AVAssetExportSession?

    func export() {
        exporter = AVAssetExportSession(asset: mixComposition, presetName: AVAssetExportPresetHighestQuality)
        guard exporter != nil else { return }
        exporter.outputFileType = AVFileTypeMPEG4
        exporter.outputURL = url
        exporter.videoComposition = mainComposition
        print("done")

        exporter?.exportAsynchronously(completionHandler: { 
            DispatchQueue.main.async {
               self.exportDidFinish(exporter)
               print("removing AI")
               self.removeAI()
               print("removed AI")
               completion()
           }
        }
    }
}

我真的不确定为什么还要将所有内容都放在完成 block 中的主线程上,但也许你想稍后对 UI 做一些事情,所以我把它留在那里。

但最重要的是 - 让你的 AVAssetExportSession 不存储在可能释放它的方法上。这是一个可能导致此问题的内存问题。

关于ios - 应用程序崩溃, "Lost Connection",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45777587/

相关文章:

ios - 如果用户意外拒绝了位置服务,该如何获取位置服务以提示他们获得位置许可?

ios - 所有构建的 iOS 应用程序都会收到错误 "The application could not be verified"

ios - UIImageView 保持宽高比,但适合宽度

iOS 应用程序上传到 iTunes Connect 导致无效签名问题

ios - 在预填充的 TableView 上显示 NSArray

ios - UIActivityViewController - 插件 net.whatsapp.WhatsApp.ShareExtension 无效

ios - NEHotspotConfiguration WPA/WPA2 密码长度无效

ios - Swift Sprite Kit 重新创建 SKSpriteNode`s

iphone - 如何在 iPhone 中 Ping 或检查特定 IP 地址的服务器是否可用/已连接

iphone - 禁止在受监管设备上进行操作