CGImageCreate测试模式不起作用(iOS) [英] CGImageCreate Test Pattern is not working (iOS)

查看:861
本文介绍了CGImageCreate测试模式不起作用(iOS)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为iOS 5.1设备创建UIImage测试模式。目标UIImageView的大小为320x240,但我试图创建一个160x120 UIImage测试模式(未来,非测试模式图像将是这个大小)。我希望盒子的上半部分是蓝色而下半部分是红色的,但我看起来像未初始化的内存会破坏图像的底部。代码如下:

I'm trying to create an UIImage test pattern for an iOS 5.1 device. The target UIImageView is 320x240 in size, but I was trying to create a 160x120 UIImage test pattern (future, non-test pattern images will be this size). I wanted the top half of the box to be blue and the bottom half to be red, but I get what looks like uninitialized memory corrupting the bottom of the image. The code is as follows:

int width = 160;
int height = 120;
unsigned int testData[width * height];
for(int k = 0; k < (width * height) / 2; k++)
    testData[k] = 0xFF0000FF;   // BGRA (Blue)
for(int k = (width * height) / 2; k < width * height; k++)
    testData[k] = 0x0000FFFF;   // BGRA (Red)

int bitsPerComponent = 8;
int bitsPerPixel = 32;
int bytesPerRow = 4 * width;
CGDataProviderRef provider = CGDataProviderCreateWithData(NULL, &testData, (width * height * 4), NULL);
CGColorSpaceRef colorSpaceRef = CGColorSpaceCreateDeviceRGB();      
CGBitmapInfo bitmapInfo = kCGImageAlphaNoneSkipFirst;
CGColorRenderingIntent renderingIntent = kCGRenderingIntentDefault;

CGImageRef imageRef = CGImageCreate(width, height, bitsPerComponent, bitsPerPixel, bytesPerRow, 
                                    colorSpaceRef, bitmapInfo, provider, NULL, NO,renderingIntent);

UIImage *myTestImage = [UIImage imageWithCGImage:imageRef];

这应该看起来像Stack Overflow上的另一个例子。无论如何,我发现当我减小测试图案的大小时,图像的损坏部分会增加。同样奇怪的是,我在腐败部分看到了红线,所以看起来我只是弄乱了组件的大小。我错过了什么?感觉就像提供者中的某些东西,但我没有看到它。

This should look like another example on Stack Overflow. Anyway, I found that as I decrease the size of the test pattern the "corrupt" portion of the image increases. What is also strange is that I see lines of red in the "corrupt" portion, so it doesn't appear that I'm just messing up the sizes of components. What am I missing? It feels like something in the provider, but I don't see it.

谢谢!

添加了截图。以下是kCGImageAlphaNoneSkipFirst设置的样子:

Added screenshots. Here is what it looks like with kCGImageAlphaNoneSkipFirst set:

以下是kCGImageAlphaFirst的样子:

And here is what it looks like with kCGImageAlphaFirst:

推荐答案

您的像素数据是一个自动变量,所以它存储在堆栈中:

Your pixel data is in an automatic variable, so it's stored on the stack:

unsigned int testData[width * height];

您必须从声明此数据的函数返回。这使得函数的堆栈帧被其他函数弹出并重用,这会覆盖数据。

You must be returning from the function where this data is declared. That makes the function's stack frame get popped and reused by other functions, which overwrites the data.

但是,您的图像仍然是指同一地址上的像素数据堆栈。 ( CGDataProviderCreateWithData 不会复制数据,只是引用它。)

Your image, however, still refers to that pixel data at the same address on the stack. (CGDataProviderCreateWithData doesn't copy the data, it just refers to it.)

要修复:使用 malloc CFMutableData NSMutableData 为像素数据分配空间堆。

To fix: use malloc or CFMutableData or NSMutableData to allocate space for your pixel data on the heap.

这篇关于CGImageCreate测试模式不起作用(iOS)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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