如何从字节创建UIImage? [英] How create UIImage from bytes?

查看:125
本文介绍了如何从字节创建UIImage?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在UIImage中根据我的颜色创建(例如,我需要图像1x1像素,黑色)。

I need in UIImage created from my colors (for example, i need in image 1x1 pixel with black color).

我有阵列:

unsigned char *color[] = {0, 0, 0, 1};

如何从这个数组创建UIImage?

How can i create UIImage from this array ?

我试过

 unsigned char *bytes[4] = {0,0,0,1};
 NSData *data = [NSData dataWithBytes:bytes length:4];
 UIImage *img = [UIImage imageWithData:data];

但此方法没有结果......

but this method has no result...

推荐答案

您需要创建一个位图上下文(使用CGBitmapContextCreate()),然后绘制它。这需要使用 CoreGraphics 。然后获取您的上下文并使用CGBitmapContextCreateImage()从中创建CGImageRef。最后,使用+ [UIImage imageWithCGImage:]将CGImageRef转换为UIImage。

You'll want to create a bitmap context (using CGBitmapContextCreate()), and draw into that. This requires using CoreGraphics. Then take your context and use CGBitmapContextCreateImage() to create an CGImageRef out of it. Finally, use +[UIImage imageWithCGImage:] to convert that CGImageRef to a UIImage.

这篇关于如何从字节创建UIImage?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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