LoginSignup
12
12

More than 5 years have passed since last update.

CMSampleBufferをCGImageやUIImageに変換

Last updated at Posted at 2012-12-26

AVCaptureSession等でカメラ画像をキャプチャリングした場合には、CMSampleBufferの形式で画像が得られる。
これをビットマップ処理する場合などに、ビットマップデータまたはCGImage等でアクセスしたい場合に有効。


CVImageBufferRef imageBuffer = CMSampleBufferGetImageBuffer(sampleBuffer); 
CVPixelBufferLockBaseAddress(imageBuffer, 0);
void *baseAddress = CVPixelBufferGetBaseAddressOfPlane(imageBuffer, 0);
size_t bytesPerRow = CVPixelBufferGetBytesPerRow(imageBuffer); 
size_t width       = CVPixelBufferGetWidth(imageBuffer);
size_t height      = CVPixelBufferGetHeight(imageBuffer);
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef newContext = CGBitmapContextCreate(baseAddress, width, height, 8, bytesPerRow, colorSpace, kCGBitmapByteOrder32Little | kCGImageAlphaPremultipliedFirst);
CGImageRef cgImage = CGBitmapContextCreateImage(newContext); 
UIImage *uiImage = [UIImage imageWithCGImage:cgImage scale:1.0f orientation:UIImageOrientationRight];
CGContextRelease(newContext); 
CGColorSpaceRelease(colorSpace); 
CGImageRelease(cgImage);
12
12
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
12
12