Исходя из моего предыдущего вопроса о Crop and Scale CMSampleBufferRef , я нашел способ изменить CGContext внутри этого CMSampleBufferRef, и я могу нарисовать прямоугольник, путь, круг и очистить прямоугольник 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);
, но не успешно.
Можеткто-нибудь дает мне еще несколько советов здесь