gpt4 book ai didi

ios - 创建图像像素缓冲区时内存泄漏

转载 作者:搜寻专家 更新时间:2023-11-01 05:38:59 26 4
gpt4 key购买 nike

我正在尝试使用 AVAssetWriterInput 创建带有图像输入的视频,为此我需要创建图像的像素缓冲区。

为此,我调用了下面的函数,但在创建了一些视频后,该应用程序收到了内存警告并崩溃了。我已经使用 Instruments 对其进行了调试,但似乎存在内存泄漏 here .

我曾尝试将变量 pixelBufferPointer 和 pxData 作为类变量,并在视频创建后销毁/解除分配,但这似乎没有任何区别。我应该做些什么来释放这段内存吗?

func createPixelBufferFromCGImage(image: CGImageRef) -> CVPixelBufferRef {
let options = [
"kCVPixelBufferCGImageCompatibilityKey": true,
"kCVPixelBufferCGBitmapContextCompatibilityKey": true
]

var videoWidth = 496
var videoHeight = 668

let frameSize = CGSizeMake(CGFloat(videoWidth), CGFloat(videoHeight))

var pixelBufferPointer = UnsafeMutablePointer<Unmanaged<CVPixelBuffer>?>.alloc(1)

var status:CVReturn = CVPixelBufferCreate(
kCFAllocatorDefault,
Int(frameSize.width),
Int(frameSize.height),
OSType(kCVPixelFormatType_32ARGB),
options,
pixelBufferPointer
)

var lockStatus:CVReturn = CVPixelBufferLockBaseAddress(pixelBufferPointer.memory?.takeUnretainedValue(), 0)

var pxData:UnsafeMutablePointer<(Void)> = CVPixelBufferGetBaseAddress(pixelBufferPointer.memory?.takeUnretainedValue())

let bitmapinfo = CGBitmapInfo(rawValue: CGImageAlphaInfo.NoneSkipFirst.rawValue)
let rgbColorSpace:CGColorSpace = CGColorSpaceCreateDeviceRGB()

var context:CGContextRef = CGBitmapContextCreate(
pxData,
Int(frameSize.width),
Int(frameSize.height),
8,
//4 * CGImageGetWidth(image),
4 * Int(frameSize.width),
rgbColorSpace,
bitmapinfo
)

CGContextDrawImage(context, CGRectMake(0, 0, frameSize.width, frameSize.height), image)

CVPixelBufferUnlockBaseAddress(pixelBufferPointer.memory?.takeUnretainedValue(), 0)

UIGraphicsEndImageContext()

return pixelBufferPointer.memory!.takeUnretainedValue()
}

最佳答案

Unmanaged<CVPixelBuffer>正在泄漏。

我更喜欢 Russell Austin 的回答,但我不知道如何通过 pixelBufferPointerCVPixelBufferCreate没有语法错误(快速新手)。否则,更改线路

return pixelBufferPointer.memory!.takeUnretainedValue()

return pixelBufferPointer.memory!.takeRetainedValue()

修复泄漏

关于ios - 创建图像像素缓冲区时内存泄漏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33110484/

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