gpt4 book ai didi

c++ - 如何从自定义数据流编写 CoreGraphics CGImageRef?

转载 作者:搜寻专家 更新时间:2023-10-31 01:04:00 25 4
gpt4 key购买 nike

我希望以一个 Core Graphics CGImageRef 结束,它以覆盖的 -(void)drawRect:(CGRect)rect 方法绘制到屏幕上。这是我到目前为止所得到的。

- (void)drawRect:(CGRect)rect
{
CGContextRef currentContext = UIGraphicsGetCurrentContext();

const CGFloat _x = CGRectGetMinX(rect);
const CGFloat _y = CGRectGetMinY(rect);
const CGFloat _w = CGRectGetWidth(rect);
const CGFloat _h = CGRectGetHeight(rect);

const CGFloat scale = [[UIScreen mainScreen] scale];

const int pixelsWide = (_w * scale);
const int pixelsHigh = (_h * scale);

const size_t colorValues = 4;
const int rawMemorySize = (pixelsWide * pixelsHigh * colorValues);

// raw pixel data memory
UInt8 pixelData[rawMemorySize];

// fill the raw pixel buffer with arbitrary gray color for test

for(size_t ui = 0; ui < rawMemorySize; ui++)
{
pixelData[ui] = 255; // black
}

CGColorSpaceRef colorspace = CGColorSpaceCreateDeviceRGB();
CFDataRef rgbData = CFDataCreate(NULL, pixelData, rawMemorySize);
CGDataProviderRef provider = CGDataProviderCreateWithCFData(rgbData);

CGImageRef rgbImageRef = CGImageCreate(pixelsWide, pixelsHigh, 8, (colorValues * colorValues), (pixelsWide * colorValues), colorspace, kCGBitmapByteOrderDefault, provider, NULL, true, kCGRenderingIntentDefault);

CFRelease(rgbData);
CGDataProviderRelease(provider);
CGColorSpaceRelease(colorspace);

//Draw the bitmapped image
CGContextDrawImage(currentContext, CGRectMake(_x, _y, pixelsWide, 64), rgbImageRef);
CGImageRelease(rgbImageRef);
}

我在 for 循环中抛出一个 EXC_BAD_ACCESS 错误。

我想我很接近,但我不太确定。任何想法可能是罪魁祸首?提前谢谢你:)

最佳答案

还有一些事情需要注意。

drawRect 的rect 参数只是脏矩形,它可能小于self.bounds。使用 self.bounds 除非你的代码被优化为只绘制脏矩形。

在堆栈上分配 pixelData 数组是个坏主意,并且可能是错误的原因。这完全取决于 View 有多大。我怀疑 AaronGolden 使用的 View 比您使用的 View 小,因此没有任何问题。 calloc 数组更安全,并在结束使用后释放它。请注意,calloc 会将数组清除为透明黑色。

这是我过去用来做这类事情的一些示例代码。即使像素数组是使用 calloc 创建的,您仍然可以像访问标准数组一样访问它(例如 pixels[100] = 0xff0000ff; 是完全有效的)。但是,出于性能原因,我通常使用指针来访问单个像素。

const CGFloat scale = [[UIScreen mainScreen] scale];
int width = self.bounds.size.width * scale;
int height = self.bounds.size.height * scale;

uint32_t *pixels = calloc( width * height, sizeof(uint32_t) );
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef context = CGBitmapContextCreate( pixels, width, height, 8, width * sizeof(uint32_t), colorSpace, kCGBitmapByteOrder32Big | kCGImageAlphaPremultipliedLast );

uint8_t *bufptr = (uint8_t *)pixels;
for ( int y = 0; y < height; y++)
{
for ( int x = 0; x < width; x++ )
{
bufptr[0] = redValue;
bufptr[1] = greenValue;
bufptr[2] = blueValue;
bufptr[3] = alphaValue;

bufptr += 4;
}
}

CGImageRef newCGImage = CGBitmapContextCreateImage( context );
CGContextRelease( context );
CGColorSpaceRelease( colorSpace );
free( pixels );

关于c++ - 如何从自定义数据流编写 CoreGraphics CGImageRef?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24792899/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com