3

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

我正在尝试将许多 AVAsset 的短间隔合并到一个视频文件中。我需要对它们应用额外的过滤器和转换。

我实现了类,它可以采用一种资产并完全按照我的意愿制作所有东西,但是现在,当我尝试对许多(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)
            }
        }
    }
4

1 回答 1

0

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

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

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

[unowned self]您可以从以下链接 中找到更多信息:我们应该在 Swift 中始终在闭包内使用 [unowned self]

添加后,在您的类中[unowned self]添加deinit(){ }函数并释放或消除不需要的数据。

于 2018-04-19T05:49:23.973 回答