2

我正在编写一个将 AVFoundation 用于相机内容的 iPhone 应用程序,并且我正在尝试将 UIImage 从相机保存到相机胶卷中。

它目前是这样做的......

[imageCaptureOutput captureStillImageAsynchronouslyFromConnection:[imageCaptureOutput.connections objectAtIndex:0]
             completionHandler:^(CMSampleBufferRef imageDataSampleBuffer, NSError *error)
 {
  if (imageDataSampleBuffer != NULL)
  {
   NSData *imageData = [AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:imageDataSampleBuffer];
   UIImage *image = [[UIImage alloc] initWithData:imageData];

   MyCameraAppDelegate *delegate = [[UIApplication sharedApplication] delegate];

   [delegate processImage:image];
  }
 }];

我一直在观看 WWDC 教程视频,我认为从 imageDataSampleBuffer 到 UIImage 的 2 行(NSData... 和 UIImage...)是一个漫长的过程。

将图像保存到库似乎需要很长时间。

有谁知道是否有单行转换可以让 UIImage 脱离这个?

谢谢你的帮助!

奥利弗

4

1 回答 1

3

我认为在完成处理程序块中执行此操作可能会更有效,但你是对的,它节省了花费最多时间的库。

CVImageBufferRef imageBuffer = CMSampleBufferGetImageBuffer(imageDataSampleBuffer);

CVPixelBufferLockBaseAddress(imageBuffer, 0); 
uint8_t *baseAddress = (uint8_t *)CVPixelBufferGetBaseAddress(imageBuffer); 
size_t bytesPerRow = CVPixelBufferGetBytesPerRow(imageBuffer); 
size_t width = CVPixelBufferGetWidth(imageBuffer); 
size_t height = CVPixelBufferGetHeight(imageBuffer); 
CVPixelBufferUnlockBaseAddress(imageBuffer, 0);
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); 
CGContextRef context = CGBitmapContextCreate(baseAddress, width, height, 8, bytesPerRow, colorSpace, kCGBitmapByteOrder32Little | kCGImageAlphaPremultipliedFirst); 
CGImageRef cgImage = CGBitmapContextCreateImage(context); 
CGContextRelease(context);
CGColorSpaceRelease(colorSpace);

if ( /*wanna save metadata on iOS4.1*/ ) {
  CFDictionaryRef metadataDict = CMCopyDictionaryOfAttachments(NULL, imageDataSampleBuffer, kCMAttachmentMode_ShouldPropagate);
  [assetsLibraryInstance writeImageToSavedPhotosAlbum:cgImage metadata:metadataDict completionBlock:^(NSURL *assetURL, NSError *error) { /*do something*/ }];
  CFRelease(metadataDict);
} else {
  [assetsLibraryInstance writeImageToSavedPhotosAlbum:cgImage orientation:ALAssetOrientationRight completionBlock:^(NSURL *assetURL, NSError *error) { /*do something*/ }];
  // i think this is the correct orientation for Portrait, or Up if deviceOr'n is L'Left, Down if L'Right
}
CGImageRelease(cgImage);
于 2010-09-20T23:32:41.707 回答