2010-09-05 19 views
2

我正在寫一個iPhone應用程序,使用AVFoundation的相機的東西,我試圖將UIImage從相機保存到相機膠捲。讓UIImage走出捕捉殭屍圖像AsynchronouslyFromConnection

目前,它確實是這樣......

[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?

感謝您的幫助!

奧利弗

+0

啊,我仔細看了一下發生了什麼事情,這不是花費很長時間的轉換,而是將它保存到Photo庫中。 – Fogmeister 2010-09-06 12:48:39

回答

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

這在iOS9.0中返回一個零CVImageBufferRef。 – loretoparisi 2015-09-20 22:13:14