Quiero lanzar CVPixelBufferRef en swift

Quiero crear un video a partir de la image.
Entonces, fui la fuente del enlace a la reference.
Enlace: CVPixelBufferPool Error (kCVReturnInvalidArgument / -6661)

func writeAnimationToMovie(path: String, size: CGSize, animation: Animation) -> Bool { var error: NSError? let writer = AVAssetWriter(URL: NSURL(fileURLWithPath: path), fileType: AVFileTypeQuickTimeMovie, error: &error) let videoSettings = [AVVideoCodecKey: AVVideoCodecH264, AVVideoWidthKey: size.width, AVVideoHeightKey: size.height] let input = AVAssetWriterInput(mediaType: AVMediaTypeVideo, outputSettings: videoSettings) let pixelBufferAdaptor = AVAssetWriterInputPixelBufferAdaptor(assetWriterInput: input, sourcePixelBufferAttributes: nil) input.expectsMediaDataInRealTime = true writer.addInput(input) writer.startWriting() writer.startSessionAtSourceTime(kCMTimeZero) var buffer: CVPixelBufferRef var frameCount = 0 for frame in animation.frames { let rect = CGRectMake(0, 0, size.width, size.height) let rectPtr = UnsafeMutablePointer<CGRect>.alloc(1) rectPtr.memory = rect buffer = pixelBufferFromCGImage(frame.image.CGImageForProposedRect(rectPtr, context: nil, hints: nil).takeUnretainedValue(), size) var appendOk = false var j = 0 while (!appendOk && j < 30) { if pixelBufferAdaptor.assetWriterInput.readyForMoreMediaData { let frameTime = CMTimeMake(Int64(frameCount), 10) appendOk = pixelBufferAdaptor.appendPixelBuffer(buffer, withPresentationTime: frameTime) // appendOk will always be false NSThread.sleepForTimeInterval(0.05) } else { NSThread.sleepForTimeInterval(0.1) } j++ } if (!appendOk) { println("Doh, frame \(frame) at offset \(frameCount) failed to append") } } input.markAsFinished() writer.finishWritingWithCompletionHandler({ if writer.status == AVAssetWriterStatus.Failed { println("oh noes, an error: \(writer.error.description)") } else { println("hrmmm, there should be a movie?") } }) return true; } 

 func pixelBufferFromCGImage(image: CGImageRef, size: CGSize) -> CVPixelBufferRef { let options = [ kCVPixelBufferCGImageCompatibilityKey: true, kCVPixelBufferCGBitmapContextCompatibilityKey: true] var pixBufferPointer = UnsafeMutablePointer<Unmanaged<CVPixelBuffer>?>.alloc(1) let status = CVPixelBufferCreate( nil, UInt(size.width), UInt(size.height), OSType(kCVPixelFormatType_32ARGB), options, pixBufferPointer) CVPixelBufferLockBaseAddress(pixBufferPointer.memory?.takeUnretainedValue(), 0) let rgbColorSpace = CGColorSpaceCreateDeviceRGB() let bitmapinfo = CGBitmapInfo.fromRaw(CGImageAlphaInfo.NoneSkipFirst.toRaw()) var pixBufferData:UnsafeMutablePointer<(Void)> = CVPixelBufferGetBaseAddress(pixBufferPointer.memory?.takeUnretainedValue()) let context = CGBitmapContextCreate( pixBufferData, UInt(size.width), UInt(size.height), 8, UInt(4 * size.width), rgbColorSpace, bitmapinfo!) CGContextConcatCTM(context, CGAffineTransformMakeRotation(0)) CGContextDrawImage( context, CGRectMake(0, 0, CGFloat(CGImageGetWidth(image)), CGFloat(CGImageGetHeight(image))), image) CVPixelBufferUnlockBaseAddress(pixBufferPointer.memory?.takeUnretainedValue(), 0) return pixBufferPointer.memory!.takeUnretainedValue() 

}

Algo incluso después de que las películas pueden ser como debajo de la image permanece en la memory. Creo o no que queda PixcelBuffer.

Tenía un método CVPixelBufferRelease (búfer) para liberar el PixcelBuffer cuando el Objective-c, ya no puedo usar esto en Swift. ¿Cómo presento el PixcelBuffer?

Si alguien pudiera ayudar, realmente lo agradecería.

1 introduzca la descripción de la imagen aquí

2 introduzca la descripción de la imagen aquí

Al usar CVPixelBufferCreate el UnsafeMutablePointer se debe destruir después de recuperar la memory .

Cuando creo un CVPixelBuffer , lo hago así:

 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 } 

Tuve el mismo problema, pero lo he resuelto.

Use esto: autoreleasepool

  var boolWhile = true while (boolWhile) { autoreleasepool({() -> () in if(input.readyForMoreMediaData) { presentTime = CMTimeMake(Int64(ii), fps) if(ii >= arrayImages.count){ ... 

Prueba cambiar

  return pixBufferPointer.memory!.takeUnretainedValue() 

a

 return pixBufferPointer.memory!.takeRetainedValue() 

para evitar la fuga de CVPixelBuffer