【问题标题】:Crop and Scale CGContext - iOS裁剪和缩放 CGContext - iOS
【发布时间】: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


    【解决方案1】:

    创建后不能更改 CVImageBuffer 的尺寸。如果你需要改变尺寸,你必须创建一个新的缓冲区。

    如果要裁剪图像,然后将裁剪部分缩放到原始图像的大小,则需要将缩放操作执行到第二个缓冲区,然后从第二个缓冲区复制回第一个缓冲区。

    【讨论】:

    • 您的意思是我更改了 CVContextRef 并使用新的 CVContextRef 重新创建新的 CMSampleBufferRef?
    • 您好,我该怎么做?你有示例代码吗,我研究了一段时间后确实需要这个
    猜你喜欢
    • 2015-02-15
    • 2012-07-21
    • 2015-06-20
    • 1970-01-01
    • 1970-01-01
    • 2015-01-21
    • 2017-06-18
    • 2013-08-02
    • 2019-04-08
    相关资源
    最近更新 更多