如果CGImageCreate的数据提供者使用应用程序创建的数组,那么对CGImageCreate的正确调用是什么样的呢? [英] What does a correct call to CGImageCreate look like if the data provider for it uses an array created by the app?

查看:343
本文介绍了如果CGImageCreate的数据提供者使用应用程序创建的数组,那么对CGImageCreate的正确调用是什么样的呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在内存中创建一个位图作为模式函数的一部分,drawLayer:inContext:方法(此方法是CALayer委托协议的一部分)将调用。模式函数看起来类似于:

I'm trying to create a bitmap in memory as part of a pattern function that a drawLayer:inContext: method (this method is part of the CALayer delegate protocol) will call. The pattern function looks similar to this:

static const size_t kComponentsPerPixel = 4;
static const size_t kBitsPerComponent = sizeof(unsigned char) * 8;

NSInteger layerHeight = 160;
NSInteger layerWidth = 160;
CGContextSaveGState(context); 

CGColorSpaceRef rgb = CGColorSpaceCreateDeviceRGB();

size_t bufferLength = layerWidth * layerHeight * kComponentsPerPixel;

unsigned char *buffer = malloc(bufferLength);

// The real function does something more interesting with the buffer, but I cut it 
// to reduce the complexity while I figure out the crash.
for (NSInteger i = 0; i < bufferLength; ++i)
{
    buffer[i] = 255;
}
//memset(buffer, 255, bufferLength);

CGDataProviderRef provider = 
CGDataProviderCreateWithData(NULL, &buffer, bufferLength, NULL);//freeBitmapBuffer);

CGImageRef imageRef = 
CGImageCreate(layerWidth, layerHeight, kBitsPerComponent, 
              kBitsPerComponent * kComponentsPerPixel, 
              kComponentsPerPixel * layerWidth, 
              rgb, 
              kCGBitmapByteOrderDefault | kCGImageAlphaLast, 
              provider, NULL, false, kCGRenderingIntentDefault);

CGContextDrawImage(context, CGRectMake(0, 0, 160, 160), imageRef);

CGImageRelease(imageRef);
CGDataProviderRelease(provider);
CGColorSpaceRelease(rgb);     

CGContextRestoreGState(context);

稍后,当drawLayer:inContext:调用CGContextFillRect来显示由此函数创建的模式时,我得到EXC_BAD_ACCESS 。堆栈的顶部是CGSConvertAlphaByte。那时我查看了缓冲区的内存,看起来很好 - 设置为调用模式函数时的设置。

Later, when drawLayer:inContext: calls CGContextFillRect to display the pattern created by this function, I get EXC_BAD_ACCESS. The top of the stack is CGSConvertAlphaByte. I looked at the buffer's memory at that point, and it seemed fine - set to exactly what it was set to when the pattern function was called.

我想也许我弄乱了CGImageCreate的一些参数,很可能是旗帜。或者缓冲区没有以正确的顺序填充,但我不确定如果我用相同的值填充每个字节我怎么会出错。

I think that maybe I messed up some parameter of CGImageCreate, quite possibly the flags. Or the buffer is not populated in the right order, but I'm not sure how I can go wrong if I fill every byte with the same value.

任何想法或非崩溃类似代码的例子?

Any ideas or examples of similar code that is non-crashing?

推荐答案

好的,所以Apple dev论坛发现了这个错误:我正在传递& ;缓冲区由于某种原因而不是缓冲区。

OK, so the Apple dev forums spotted the mistake: I was passing &buffer for some reason instead of buffer.

这篇关于如果CGImageCreate的数据提供者使用应用程序创建的数组,那么对CGImageCreate的正确调用是什么样的呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆