iPhone - UIImage imageWithData 返回 nil
我需要从字节数组创建 UIImage。 现在我创建了字节数组:
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);
数据变量是一个无符号字符。
这就是我尝试获取 UIImage 的方式:
NSData *imgData = [NSData dataWithBytes:data[i][j] length:TILE_WIDTH*TILE_HEIGHT*numberOfCompponents];
UIImage *img = [UIImage imageWithData: imgData];
img (UIImage) 保持为零。
好的,这不是背景:我正在尝试创建一个像素化应用程序:)。 iPhone 4 相机拍摄的图像太大,因此我将图像分割成较小的图像。这样做,当需要更新像素化(触摸)区域以显示像素化效果时,我将更新一个较小的 UIImage。我需要这样做,因为在之前的测试中,UIImage 的更新似乎正在消耗 CPU。尽管如此,较小的图像现在约为 80x100 像素,并且更新工作并不尽可能顺利。有时,如果你快速移动手指,它会错过一些点:D。我认为使用这种方法从字节数组创建 UIImage 比这个方法更快:
CGImageRef cgImage = CGImageCreate(TILE_WIDTH, TILE_HEIGHT, bitsPerComponent,
bitsPerPixel, bitmapBytesPerRow, colorSpace, kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big,
dataProvider, NULL, false, kCGRenderingIntentDefault);
UIImage *imageToBeUpdated = [UIImage imageWithCGImage:cgImage];
我是对的吗?
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.
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];
The img (UIImage) is remaining nil.
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];
Am I correct?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
[UIImage imageWithData:data]
解析已知图像文件格式的数据(例如 jpeg、png 或 gif;完整列表位于 文档。您正在向其传递原始像素数据,这是不受支持的。请尝试使用此代替
CGBitmapContextGetData
从上下文中获取图像:[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.Try this instead of
CGBitmapContextGetData
to get the image out of the context: