iPhone - UIImage imageWithData 返回 nil [英] iPhone - UIImage imageWithData returning nil

查看:237
本文介绍了iPhone - UIImage imageWithData 返回 nil的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从字节数组创建一个 UIImage.现在我创建字节数组:

I need to create an UIImage from a byte array. Here is now I create the byte array:

image = CGImageCreateWithImageInRect(aux.CGImage, imageRect);
context = CGBitmapContextCreate (data[i][j], TILE_WIDTH, TILE_HEIGHT, 
                                 bitsPerComponent, bitmapBytesPerRow, colorSpace,
                                 kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);//kCGImageAlphaNoneSkipFirst);//kCGImageAlphaNone);//

CGContextDrawImage(context, CGRectMake(0, 0, TILE_WIDTH, TILE_HEIGHT), image);

data[i][j] = CGBitmapContextGetData (context);

数据变量是一个无符号字符.

The data variable is an unsigned char.

这就是我尝试获取 UIImage 的方式:

And this is how I try to get the UIImage:

NSData *imgData = [NSData dataWithBytes:data[i][j] length:TILE_WIDTH*TILE_HEIGHT*numberOfCompponents];
        UIImage *img = [UIImage imageWithData: imgData];

img (UIImage) 仍然为零.

The img (UIImage) is remaining nil.

好的,这不是背景:我正在尝试创建一个像素化应用程序:).来自 iPhone 4 相机的图像尺寸太大,因此我将图像拆分为较小的图像.这样做,当需要更新像素化(触摸)区域以显示像素化效果时,我正在更新较小的 UIImage.我需要这样做,因为在之前的测试中,它就像 UIImage 的更新正在杀死 CPU 一样.尽管如此,较小的图像现在大约为 80x100 像素,并且更新工作并不顺利.有时,如果你快速移动手指,它会错过一些点:D.我认为使用这种方法从字节数组创建一个 UIImage 比这个更快:

OK, not this is the background: I am trying to create a pixelate application :). The images from the iPhone 4 camera are too big in size, so I split the image in smaller images. Doing so, when the area pixelated (touched) needs to be updated in order for the pixelate effect to be displayed, I am updating a smaller UIImage. I needed to do it like this because in previous tests it seamed like the update of an UIImage is killing the CPU. Still, the smaller images are now around 80x100 pxl, and the update is not working as smooth as possible. Sometimes, if you move the finger to fast it misses some spots :D. I think that using this method to create an UIImage from the byte array is faster than this one:

CGImageRef cgImage = CGImageCreate(TILE_WIDTH, TILE_HEIGHT, bitsPerComponent,
                                   bitsPerPixel, bitmapBytesPerRow, colorSpace, kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big, 
                                   dataProvider, NULL, false, kCGRenderingIntentDefault);
UIImage *imageToBeUpdated = [UIImage imageWithCGImage:cgImage];

我说得对吗?

推荐答案

[UIImage imageWithData:data] 解析已知图像文件格式(例如 jpeg、png 或 gif;完整格式)的数据文档中的列表.您'正在向它传递原始像素数据,这是不受支持的.

[UIImage imageWithData:data] parses data that is in a known image file format (e.g. jpeg, png, or gif; full list in the documentation. You're passing it raw pixel data, which is not supported.

试试这个而不是 CGBitmapContextGetData 来从上下文中获取图像:

Try this instead of CGBitmapContextGetData to get the image out of the context:

CGImageRef imgRef = CGBitmapContextCreateImage(context);
UIImage *img = [UIImage imageWithCGImage:imgRef];

这篇关于iPhone - UIImage imageWithData 返回 nil的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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