How create UIImage from bytes?

2019-07-17 05:34发布

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

I've got array:

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

How can i create UIImage from this array ?

I've try

 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...

2条回答
祖国的老花朵
3楼-- · 2019-07-17 06:11

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.

查看更多
登录 后发表回答