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 チュートリアル ビデオを見てきましたが、2 行 (NSData... と UIImage...) は imageDataSampleBuffer から UIImage への長い道のりだと思います。

画像をライブラリに保存するのに時間がかかりすぎるようです。

これからUIImageを取得するための1行の遷移があるかどうかは誰にも分かりますか?

助けてくれてありがとう!

オリバー

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 に答える