2011-09-26 47 views
1


我使用Apple公司的這個衆所周知的示例代碼將相機緩存靜止圖像轉換爲UIImages。從相機緩衝區iOS中轉換圖像。使用AVFoundation捕獲靜止圖像

-(UIImage*) getUIImageFromBuffer:(CMSampleBufferRef) imageSampleBuffer{ 

// Get a CMSampleBuffer's Core Video image buffer for the media data 
CVImageBufferRef imageBuffer = CMSampleBufferGetImageBuffer(imageSampleBuffer); 

if (imageBuffer==NULL) { 
    NSLog(@"No buffer"); 
} 

// Lock the base address of the pixel buffer 
if((CVPixelBufferLockBaseAddress(imageBuffer, 0))==kCVReturnSuccess){ 
    NSLog(@"Buffer locked successfully"); 
} 

void *baseAddress = CVPixelBufferGetBaseAddress(imageBuffer); 

// Get the number of bytes per row for the pixel buffer 
size_t bytesPerRow = CVPixelBufferGetBytesPerRow(imageBuffer); 
NSLog(@"bytes per row %zu",bytesPerRow); 
// Get the pixel buffer width and height 
size_t width = CVPixelBufferGetWidth(imageBuffer); 
NSLog(@"width %zu",width); 

size_t height = CVPixelBufferGetHeight(imageBuffer); 
NSLog(@"height %zu",height); 

// Create a device-dependent RGB color space 
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); 

// Create a bitmap graphics context with the sample buffer data 
CGContextRef context = CGBitmapContextCreate(baseAddress, width, height, 8, 
bytesPerRow, colorSpace, kCGBitmapByteOrder32Little | kCGImageAlphaPremultipliedFirst); 

// Create a Quartz image from the pixel data in the bitmap graphics context 
CGImageRef quartzImage = CGBitmapContextCreateImage(context); 

// Free up the context and color space 
CGContextRelease(context); 
CGColorSpaceRelease(colorSpace); 

// Create an image object from the Quartz image 
UIImage *image= [UIImage imageWithCGImage:quartzImage scale:SCALE_IMAGE_RATIO orientation:UIImageOrientationRight]; 

// Release the Quartz image 
CGImageRelease(quartzImage); 

// Unlock the pixel buffer 
CVPixelBufferUnlockBaseAddress(imageBuffer,0); 


return (image);} 

問題是,通常您獲得的圖像旋轉90°。使用方法+ imageWithCGImage:scale:orientation我可以旋轉它,但在進入此方法之前,我正在嘗試使用CTM函數旋轉和縮放圖像,然後將其傳遞給UIImage。問題在於CTM轉換不影響圖像。
我在問自己爲什麼...是因爲我鎖定了緩衝區?或者是因爲上下文是與圖像內部一起創建的,所以這些更改只會影響進一步的mod?
謝謝

回答

0

答案是它隻影響進一步的修改,它沒有任何處理緩衝區鎖定。
正如你可以從這個答案中讀取mod,上下文是按時間應用的Vertical flip of CGContext

相關問題