【发布时间】:2011-12-14 08:03:59
【问题描述】:
根据我之前关于 Crop and Scale CMSampleBufferRef 的问题,我找到了一种修改 CMSampleBufferRef 内的 CGContext 的方法,并且我能够使用以下代码绘制 CGContext 的矩形、路径、圆形和透明矩形:
- (void)modifyImage:(CMSampleBufferRef) sampleBuffer {
CVImageBufferRef imageBuffer = CMSampleBufferGetImageBuffer(sampleBuffer);
// Lock the image buffer
CVPixelBufferLockBaseAddress(imageBuffer,0);
// Get information about the image
uint8_t *baseAddress = (uint8_t *)CVPixelBufferGetBaseAddress(imageBuffer);
size_t bytesPerRow = CVPixelBufferGetBytesPerRow(imageBuffer);
size_t width = CVPixelBufferGetWidth(imageBuffer);
size_t height = CVPixelBufferGetHeight(imageBuffer);
// Create a CGImageRef from the CVImageBufferRef
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef context = CGBitmapContextCreate(baseAddress, width, height, 8, bytesPerRow, colorSpace, kCGBitmapByteOrder32Little | kCGImageAlphaPremultipliedFirst);
CGContextSaveGState(context);
CGContextSetFillColorWithColor(context, [[UIColor blackColor] CGColor]);
CGContextFillRect(context, CGRectMake(0, 0, 400, 400));
//restore the context and remove the clipping area.
CGContextRestoreGState(context);
// We unlock the image buffer
CVPixelBufferUnlockBaseAddress(imageBuffer,0);
// We release some components
CGContextRelease(context);
CGColorSpaceRelease(colorSpace);
return;
}
现在,我想做的是裁剪然后缩放这个 CGContext,我尝试使用 CGContextClipToRect(context, CGRectMake(0, 0, 360, 640)); 和 CGContextScaleCTM(context, 2, 2); 但没有成功。
谁能在这里给我更多的建议
【问题讨论】:
标签: iphone objective-c ios image-processing cgcontext