title: How do I save a BGRA image to iphone and then take it to a laptop for post-processing
body: <p>I want to use iphone's camera to take a raw image data and then use matlab or opencv in the laptop to post-process it. So I need take this raw image data from the iphone to the laptop. </p>
  
  <p>Currently, I use AVFoudnation to get 32BGRA image (it is the raw image data, right?). And I save it as a file, but I cannot take it from the iphone. (Is there any solutions without jailbreak?)</p>
  
  <p>Following are some relevant codes:</p>
  
  <pre><code>    if let videoConnection = stillImageOutput!.connectionWithMediaType(AVMediaTypeVideo) {
          videoConnection.videoOrientation = AVCaptureVideoOrientation.Portrait
          stillImageOutput?.captureStillImageAsynchronouslyFromConnection(videoConnection, completionHandler: {(sampleBuffer, error) in
              if (sampleBuffer != nil) {                                   
                  // get the raw image data
                  let cameraFrame = CMSampleBufferGetImageBuffer(sampleBuffer)
  
                  CVPixelBufferLockBaseAddress(cameraFrame!, 0)
  
                  let rawImage = CVPixelBufferGetBaseAddress(cameraFrame!)
                  let bytesPerRow = CVPixelBufferGetBytesPerRow(cameraFrame!)
                  let rawImageData = NSData.init(bytes: rawImage, length: bytesPerRow*CVPixelBufferGetHeight(cameraFrame!))
  
                  // process the rawImageData, we now save it
                  var paths = NSSearchPathForDirectoriesInDomains (.DocumentDirectory, .UserDomainMask, true);
                  let documentsDirectory = paths[0]
                  let fileName = documentsDirectory.stringByAppendingString("/rawImageData")                    
  
                  do {
                      try rawImageData.writeToFile(fileName, options: NSDataWritingOptions.DataWritingFileProtectionComplete)
                  } catch {
  
                  }
                  print("Raw image data write successfully.")
                  print("Path of raw image data: ", fileName)                   
                  CVPixelBufferUnlockBaseAddress(cameraFrame!, 0)               
              }
          })
      }
  </code></pre>
  
  <p>Should I need save the BGRA data to the photo album and then take it out, if so, how can I do it (use UIImage?)</p>
  
  <p>Thanks.</p>
  
  <p>An update:</p>
  
  <p>I transform BGRA image to UIImage and use UIImageWriteToSavedPhotosAlbum to save it to the photo album, then I use airdrop to take the image file to may mac. However, I found the image is JPEG format. What's wrong?
  Following are the codes:</p>
  
  <pre><code>            if (sampleBuffer != nil) {                   
                  let rawImage:UIImage = self.imageFromSampleBuffer(sampleBuffer)
                  self.capturedImage.image = rawImage
                  UIImageWriteToSavedPhotosAlbum(rawImage, nil, nil, nil)                    
                  print("Raw Image write to photo album successfully")  
              }
  
  // Create a UIImage from sample buffer data
  func imageFromSampleBuffer(sampleBuffer : CMSampleBufferRef) -&gt; UIImage
  {
  // Get a CMSampleBuffer's Core Video image buffer for the media data
  let  imageBuffer = CMSampleBufferGetImageBuffer(sampleBuffer);
  // Lock the base address of the pixel buffer
  CVPixelBufferLockBaseAddress(imageBuffer!, 0);
  
  
  // Get the number of bytes per row for the pixel buffer
  let baseAddress = CVPixelBufferGetBaseAddress(imageBuffer!);
  
  // Get the number of bytes per row for the pixel buffer
  let bytesPerRow = CVPixelBufferGetBytesPerRow(imageBuffer!);
  // Get the pixel buffer width and height
  let width = CVPixelBufferGetWidth(imageBuffer!);
  let height = CVPixelBufferGetHeight(imageBuffer!);
  
  // Create a device-dependent RGB color space
  let colorSpace = CGColorSpaceCreateDeviceRGB();
  
  // Create a bitmap graphics context with the sample buffer data
  var bitmapInfo: UInt32 = CGBitmapInfo.ByteOrder32Big.rawValue
  bitmapInfo |= CGImageAlphaInfo.PremultipliedLast.rawValue &amp; CGBitmapInfo.AlphaInfoMask.rawValue
  let context = CGBitmapContextCreate(baseAddress, width, height, 8,
  bytesPerRow, colorSpace, bitmapInfo);
  // Create a Quartz image from the pixel data in the bitmap graphics context
  let quartzImage = CGBitmapContextCreateImage(context);
  // Unlock the pixel buffer
  CVPixelBufferUnlockBaseAddress(imageBuffer!,0);
  
  // Create an image object from the Quartz image
  let image = UIImage.init(CGImage: quartzImage!);
  
  return (image);
  }
  </code></pre>