title: I want to release the CVPixelBufferRef in swift
body: <p>I want to create a video from image.<br>
  So, I was the source of the link to the reference.<br>
  Link:<a href="https://stackoverflow.com/questions/25611086/cvpixelbufferpool-error-kcvreturninvalidargument-6661">CVPixelBufferPool Error ( kCVReturnInvalidArgument/-6661)</a><br></p>
  
  <pre><code>func writeAnimationToMovie(path: String, size: CGSize, animation: Animation) -&gt; Bool {   
      var error: NSError?
      let writer = AVAssetWriter(URL: NSURL(fileURLWithPath: path), fileType: AVFileTypeQuickTimeMovie, error: &amp;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&lt;CGRect&gt;.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 &amp;&amp; j &lt; 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;
   }
  </code></pre>
  
  <p><br></p>
  
  <pre><code>func pixelBufferFromCGImage(image: CGImageRef, size: CGSize) -&gt; CVPixelBufferRef {
      let options = [
          kCVPixelBufferCGImageCompatibilityKey: true,
          kCVPixelBufferCGBitmapContextCompatibilityKey: true]
      var pixBufferPointer = UnsafeMutablePointer&lt;Unmanaged&lt;CVPixelBuffer&gt;?&gt;.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&lt;(Void)&gt; = 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()
  </code></pre>
  
  <p>}</p>
  
  <p>Something even after the Movies can be as under the image remains in memory.
  I believe or not than is left PixcelBuffer.<br></p>
  
  <p>I had a method CVPixelBufferRelease(buffer) to release the PixcelBuffer when the Objective-c, I'm no longer can use this in Swift. How do I release the PixcelBuffer doing?<br></p>
  
  <p>If anyone could help, I'd really appreciate it.</p>
  
  <p>1
  <img src="https://i.stack.imgur.com/Lb1V4.png" alt="enter image description here"></p>
  
  <p>2
  <img src="https://i.stack.imgur.com/ZfXyj.png" alt="enter image description here"></p>