Ios 从上下文创建子图像

Ios 从上下文创建子图像,ios,core-graphics,Ios,Core Graphics,我想知道是否有一种方法可以创建与上下文中的矩形对应的CGImage 我现在正在做的是: 我正在使用CGBitmapContextCreateImage从上下文创建CGImage。然后,我使用CGImageCreateWithImageInRect提取该子图像 Anil您可以创建一个裁剪图像,如下所述 例如:- UIImage *image = //original image CGRect rect = //cropped rect CGImageRef imageRef = CGImageCr

我想知道是否有一种方法可以创建与上下文中的矩形对应的
CGImage

我现在正在做的是:

我正在使用
CGBitmapContextCreateImage
从上下文创建
CGImage
。然后,我使用
CGImageCreateWithImageInRect
提取该子图像


Anil

您可以创建一个裁剪图像,如下所述

例如:-

UIImage *image = //original image
CGRect rect = //cropped rect
CGImageRef imageRef = CGImageCreateWithImageInRect([image CGImage], rect);
UIImage *img = [UIImage imageWithCGImage:imageRef]; 
CGImageRelease(imageRef);

您需要从上下文中获取CGImage,才能使用上述代码对其进行裁剪。您可以使用问题中提到的
CGBitmapContextCreateImage
。这里是

您可以创建如下所述的裁剪图像

例如:-

UIImage *image = //original image
CGRect rect = //cropped rect
CGImageRef imageRef = CGImageCreateWithImageInRect([image CGImage], rect);
UIImage *img = [UIImage imageWithCGImage:imageRef]; 
CGImageRelease(imageRef);

您需要从上下文中获取CGImage,才能使用上述代码对其进行裁剪。您可以使用问题中提到的
CGBitmapContextCreateImage
。以下是

您可以使用分配的缓冲区创建CGBitmapContext,并使用相同的缓冲区从头开始创建CGImage。在上下文和图像共享缓冲区的情况下,您可以在上下文中绘图,然后使用主图像的该部分创建CGImage


请注意,如果随后绘制到相同的上下文中,裁剪的图像可能会实际拾取更改(取决于内部进行的共享引用而不是复制的数量)。取决于您正在做的事情,您可能会觉得这很理想,也可能会觉得不理想。

您可以使用分配的缓冲区创建CGBitmapContext,然后使用相同的缓冲区从头开始创建CGImage。在上下文和图像共享缓冲区的情况下,您可以在上下文中绘图,然后使用主图像的该部分创建CGImage

请注意,如果随后绘制到相同的上下文中,裁剪的图像可能会实际拾取更改(取决于内部进行的共享引用而不是复制的数量)。取决于你正在做什么,你可能会觉得这很理想,也可能会觉得不理想。

试试这个:

static CGImageRef createImageWithSectionOfBitmapContext(CGContextRef bigContext,
    size_t x, size_t y, size_t width, size_t height)
{
    uint8_t *data = CGBitmapContextGetData(bigContext);
    size_t bytesPerRow = CGBitmapContextGetBytesPerRow(bigContext);
    size_t bytesPerPixel = CGBitmapContextGetBitsPerPixel(bigContext) / 8;
    data += x * bytesPerPixel + y * bytesPerRow;
    CGContextRef smallContext = CGBitmapContextCreate(data,
        width, height,
        CGBitmapContextGetBitsPerComponent(bigContext), bytesPerRow,
        CGBitmapContextGetColorSpace(bigContext),
        CGBitmapContextGetBitmapInfo(bigContext));
    CGImageRef image = CGBitmapContextCreateImage(smallContext);
    CGContextRelease(smallContext);
    return image;
}
或者这个:

static CGImageRef createImageWithSectionOfBitmapContext(CGContextRef bigContext,
    size_t x, size_t y, size_t width, size_t height)
{
    uint8_t *data = CGBitmapContextGetData(bigContext);
    size_t bytesPerRow = CGBitmapContextGetBytesPerRow(bigContext);
    size_t bytesPerPixel = CGBitmapContextGetBitsPerPixel(bigContext) / 8;
    data += x * bytesPerPixel + y * bytesPerRow;
    CGDataProviderRef provider = CGDataProviderCreateWithData(NULL, data,
        height * bytesPerRow, NULL);
    CGImageRef image = CGImageCreate(width, height,
        CGBitmapContextGetBitsPerComponent(bigContext),
        CGBitmapContextGetBitsPerPixel(bigContext),
        CGBitmapContextGetBytesPerRow(bigContext),
        CGBitmapContextGetColorSpace(bigContext),
        CGBitmapContextGetBitmapInfo(bigContext),
        provider, NULL, NO, kCGRenderingIntentDefault);
    CGDataProviderRelease(provider);
    return image;
}
试试这个:

static CGImageRef createImageWithSectionOfBitmapContext(CGContextRef bigContext,
    size_t x, size_t y, size_t width, size_t height)
{
    uint8_t *data = CGBitmapContextGetData(bigContext);
    size_t bytesPerRow = CGBitmapContextGetBytesPerRow(bigContext);
    size_t bytesPerPixel = CGBitmapContextGetBitsPerPixel(bigContext) / 8;
    data += x * bytesPerPixel + y * bytesPerRow;
    CGContextRef smallContext = CGBitmapContextCreate(data,
        width, height,
        CGBitmapContextGetBitsPerComponent(bigContext), bytesPerRow,
        CGBitmapContextGetColorSpace(bigContext),
        CGBitmapContextGetBitmapInfo(bigContext));
    CGImageRef image = CGBitmapContextCreateImage(smallContext);
    CGContextRelease(smallContext);
    return image;
}
或者这个:

static CGImageRef createImageWithSectionOfBitmapContext(CGContextRef bigContext,
    size_t x, size_t y, size_t width, size_t height)
{
    uint8_t *data = CGBitmapContextGetData(bigContext);
    size_t bytesPerRow = CGBitmapContextGetBytesPerRow(bigContext);
    size_t bytesPerPixel = CGBitmapContextGetBitsPerPixel(bigContext) / 8;
    data += x * bytesPerPixel + y * bytesPerRow;
    CGDataProviderRef provider = CGDataProviderCreateWithData(NULL, data,
        height * bytesPerRow, NULL);
    CGImageRef image = CGImageCreate(width, height,
        CGBitmapContextGetBitsPerComponent(bigContext),
        CGBitmapContextGetBitsPerPixel(bigContext),
        CGBitmapContextGetBytesPerRow(bigContext),
        CGBitmapContextGetColorSpace(bigContext),
        CGBitmapContextGetBitmapInfo(bigContext),
        provider, NULL, NO, kCGRenderingIntentDefault);
    CGDataProviderRelease(provider);
    return image;
}

我将图像数据保存在
CGContext
中。我想从该上下文中的矩形区域创建一个图像,而不是从
UIImage
中创建一个图像。如果你是对的,那么你需要先创建一个CGImage,然后使用上面的代码来裁剪它。这看起来开销很大。不是吗?如果你想动态地改变环境(基于触摸或类似的实时操作),体验往往会降低。现在你必须这样做。查看文档了解更多详细信息我在
CGContext
中有图像数据。我想从该上下文中的矩形区域创建一个图像,而不是从
UIImage
中创建一个图像。如果你是对的,那么你需要先创建一个CGImage,然后使用上面的代码来裁剪它。这看起来开销很大。不是吗?如果你想动态地改变环境(基于触摸或类似的实时操作),体验往往会降低。现在你必须这样做。查看文档以了解更多详细信息
CGImage
是不可变的。创建位图数据时,它会生成位图数据的私有副本。
CGImage
是不可变的。当您创建一个位图时,它会生成位图数据的私有副本。这是一个非常优雅的解决方案,适用于我的情况!谢谢。第一个函数中有一个bug。我已经修好了。大概你用的是第二个函数,我用的是第一个函数。是的,我看到了错误,但主要的想法是我借了什么。谢谢但我必须指出一件事。在用户体验方面,我看不出我以前的实现(如问题中提到的)和现在的有多大区别。可能,瓶颈在其他地方。这是一个相当优雅的解决方案,它适用于我的情况!谢谢。第一个函数中有一个bug。我已经修好了。大概你用的是第二个函数,我用的是第一个函数。是的,我看到了错误,但主要的想法是我借了什么。谢谢但我必须指出一件事。在用户体验方面,我看不出我以前的实现(如问题中提到的)和现在的有多大区别。也许,瓶颈在其他地方。