我正在嘗試使用AVAssetWriterInput創建一個視頻,並使用AVAssetWriterInput爲其創建一個映像的像素緩衝區。創建映像的像素緩衝區時發生內存泄漏
爲此我稱之爲功能如下,但創建幾個視頻後,應用程序收到內存警告並崩潰。我使用儀器進行了調試,看起來我有內存泄漏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()
}
感謝您的快速回復。我不知道是否它是相同的錯誤,因爲我在iOS 8.3上運行此代碼,並且我也沒有使用createCGImage – carlosb
這是正確的,但我有一些嫌疑人,事實上,那些原語CVPixelBufferGetBaseAddress,CGBitmapContextCreate,CGContextDrawImage是相同的時使用使用CIContext處理createCGImage。我相信他們是相關的。 – loretoparisi