【问题标题】:getting UIImage out of captureStillImageAsynchronouslyFromConnection从 captureStillImageAsynchronouslyFromConnection 中获取 UIImage
【发布时间】:2010-09-05 00:08:09
【问题描述】:

我正在编写一个将 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 脱离这个?

感谢您的帮助!

奥利弗

【问题讨论】:

  • 啊,我仔细看看发生了什么,不是转换需要很长时间,而是将其保存到照片库。

标签: iphone avcapturesession


【解决方案1】:

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

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);

【讨论】:

  • 这在 iOS9.0 中返回一个 nil CVImageBufferRef。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-05-28
  • 2012-06-14
  • 1970-01-01
  • 2011-01-23
相关资源
最近更新 更多