Swift:来自调试器的消息:由于内存问题而终止

时间:2018-04-18 14:34:12

标签: ios swift cifilter avasset avvideocomposition

这个问题与Ios Xcode Message from debugger: Terminated due to memory issue中的不同。我正在使用不同的设备,我的应用程序在前台被杀死,除此之外我无法使用Instruments查看分配。

我正在尝试将许多AVAssets的短间隔合并为一个视频文件。我需要对它们应用其他过滤器和转换。

我实现了类,它可以占用一个资产并完全按照我的意愿制作所有内容,但是现在,当我尝试对许多内容做同样的事情时(cca 7 aasets仍然可以)更短的资产(完整的持续时间可能更短)然后使用一个资产),应用程序崩溃,我只得到“来自调试器的消息:由于内存问题而终止”日志。

我无法使用大多数Instruments工具,因为应用程序会立即崩溃。我尝试了很多东西来解决它,但我没有成功,我真的很感激一些帮助。

谢谢

相关的代码段在这里:

创作作文:

func export(toURL url: URL, callback: @escaping (_ url: URL?) -> Void){
    var lastTime = kCMTimeZero
    var instructions : [VideoFilterCompositionInstruction] = []
    let composition = AVMutableComposition()
    composition.naturalSize = CGSize(width: 1080, height: 1920)

    for (index, assetURL) in assets.enumerated() {
        let asset : AVURLAsset? = AVURLAsset(url: assetURL)
        guard let track: AVAssetTrack = asset!.tracks(withMediaType: AVMediaType.video).first else{callback(nil); return}

        let range = CMTimeRange(start: CMTime(seconds: ranges[index].lowerBound, preferredTimescale: 1000),
                                end: CMTime(seconds: ranges[index].upperBound, preferredTimescale: 1000))

        let videoTrack = composition.addMutableTrack(withMediaType: AVMediaType.video, preferredTrackID: kCMPersistentTrackID_Invalid)!
        let audioTrack = composition.addMutableTrack(withMediaType: AVMediaType.audio, preferredTrackID: kCMPersistentTrackID_Invalid)!

        do{try videoTrack.insertTimeRange(range, of: track, at: lastTime)}
        catch _{callback(nil); return}

        if let audio = asset!.tracks(withMediaType: AVMediaType.audio).first{
            do{try audioTrack.insertTimeRange(range, of: audio, at: lastTime)}
            catch _{callback(nil); return}
        }

        let layerInstruction = AVMutableVideoCompositionLayerInstruction(assetTrack: videoTrack)
        layerInstruction.trackID = videoTrack.trackID

        let instruction = VideoFilterCompositionInstruction(trackID: videoTrack.trackID,
                                                            filters: self.filters,
                                                            context: self.context,
                                                            preferredTransform: track.preferredTransform,
                                                            rotate : false)
        instruction.timeRange = CMTimeRange(start: lastTime, duration: range.duration)
        instruction.layerInstructions = [layerInstruction]

        instructions.append(instruction)

        lastTime = lastTime + range.duration
    }

    let videoComposition = AVMutableVideoComposition()
    videoComposition.customVideoCompositorClass = VideoFilterCompositor.self
    videoComposition.frameDuration = CMTimeMake(1, 30)
    videoComposition.renderSize = CGSize(width: 1080, height: 1920)
    videoComposition.instructions = instructions

    let session: AVAssetExportSession = AVAssetExportSession(asset: composition, presetName: AVAssetExportPresetHighestQuality)!
    session.videoComposition = videoComposition
    session.outputURL = url
    session.outputFileType = AVFileType.mp4

    session.exportAsynchronously(){
        DispatchQueue.main.async{
            callback(url)
        }
    }

和AVVideoCompositing类的一部分:

func startRequest(_ request: AVAsynchronousVideoCompositionRequest){
    autoreleasepool() {
        self.getDispatchQueue().sync{
            guard let instruction = request.videoCompositionInstruction as? VideoFilterCompositionInstruction else{
                request.finish(with: NSError(domain: "jojodmo.com", code: 760, userInfo: nil))
                return
            }
            guard let pixels = request.sourceFrame(byTrackID: instruction.trackID) else{
                request.finish(with: NSError(domain: "jojodmo.com", code: 761, userInfo: nil))
                return
            }

            var image : CIImage? = CIImage(cvPixelBuffer: pixels)

            for filter in instruction.filters{
                filter.setValue(image, forKey: kCIInputImageKey)
                image = filter.outputImage ?? image
            }

            let newBuffer: CVPixelBuffer? = self.renderContext.newPixelBuffer()

            if let buffer = newBuffer{
                instruction.context.render(image!, to: buffer)
                request.finish(withComposedVideoFrame: buffer)
            }
            else{
                request.finish(withComposedVideoFrame: pixels)
            }
        }
    }

1 个答案:

答案 0 :(得分:0)

当App返回内存警告时,有一些规避性。这是因为我们正在尝试处理大量数据。

为了避免这种记忆警告,我们必须养成使用[unowned self]成绩单的习惯。

如果我们在关闭时没有使用此[unowned self],那么我们将收到内存泄漏警告,并且在某个阶段,应用程序将崩溃。

您可以在以下链接的[unowned self]找到更多信息: Shall we always use [unowned self] inside closure in Swift

添加[unowned self]后,在您的课程中添加deinit(){ }功能,然后释放或删除不需要的数据。