我正在尝试使用AVAssetWriterInput创建一个带有图像输入的视频,我需要为其创建图像的像素缓冲区。
为此,我调用下面的函数,但是在创建了一些视频后,应用程序会收到内存警告并崩溃。我使用Instruments调试它,看起来我有内存泄漏here。
我曾尝试将变量pixelBufferPointer和pxData作为类变量并在创建视频后销毁/ dealloc,但这似乎没有任何区别。我应该做些什么来释放这段记忆?
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()
}
答案 0 :(得分:0)
查看Memory leak on CIContext createCGImage at iOS 9?
我有类似的问题,泄漏是由于以下代码
<!-- no doctype! -->
<!-- no HTML tag! we're all gonna die! -->
<head>
<style>
body {background:#002233;}
</style>
</head>
<head><!-- let's twist again! -->
<style>
body {color:white}
</style>
<!-- we didn't even close the second one!! -->
See this text?<br>
With the background AND color properly set?<br>
<br>
Your browser's quite a badass.
此处有更多内容相关信息:Memory Leak in CMSampleBufferGetImageBuffer
答案 1 :(得分:0)
您不必使用UnsafeMutablePointer类型。您可能正在尝试从一些Objective-C示例转换。我做了同样的事情,但最终没有找到如何做到这一点。尝试
var pixelBufferPointer: CVPixelBuffer?
...
var status:CVReturn = CVPixelBufferCreate(
kCFAllocatorDefault,
Int(frameSize.width),
Int(frameSize.height),
OSType(kCVPixelFormatType_32ARGB),
options,
&pixelBufferPointer
)
和
var pxData = CVPixelBufferGetBaseAddress(pixelBufferPointer)
然后你不必做takeUnRetainedValue等。
您也可以尝试使用AVAssetWriterInputPixelBufferAdaptor和像素缓冲池。这应该更有效率。 See this example
答案 2 :(得分:0)
Unmanaged<CVPixelBuffer>
正在泄漏。
我更喜欢Russell Austin的答案,但我无法弄清楚如何将pixelBufferPointer
传递给CVPixelBufferCreate
而没有语法错误(swift noob)。如果做不到这一点,改变行
return pixelBufferPointer.memory!.takeUnretainedValue()
到
return pixelBufferPointer.memory!.takeRetainedValue()
修复漏洞
答案 3 :(得分:0)
使用CVPixelBufferCreate
后,UnsafeMutablePointer
必须销毁后才能检索memory
。
当我创建CVPixelBuffer
时,我这样做:
func allocPixelBuffer() -> CVPixelBuffer {
let pixelBufferAttributes : CFDictionary = [...]
let pixelBufferOut = UnsafeMutablePointer<CVPixelBuffer?>.alloc(1)
_ = CVPixelBufferCreate(kCFAllocatorDefault,
Int(Width),
Int(Height),
OSType(kCVPixelFormatType_32ARGB),
pixelBufferAttributes,
pixelBufferOut)
let pixelBuffer = pixelBufferOut.memory!
pixelBufferOut.destroy()
return pixelBuffer
}