简体   繁体   English

如何从字节创建UIImage?

[英]How create UIImage from bytes?

I need in UIImage created from my colors (for example, i need in image 1x1 pixel with black color). 我需要在用我的颜色创建的UIImage中(例如,我需要在具有黑色的1x1像素的图像中)。

I've got array: 我有数组:

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

How can i create UIImage from this array ? 如何从此数组创建UIImage?

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... 但是这种方法没有结果...

You'll want to create a bitmap context (using CGBitmapContextCreate()), and draw into that. 您将要创建一个位图上下文(使用CGBitmapContextCreate()),并在其中进行绘制。 This requires using CoreGraphics . 这需要使用CoreGraphics Then take your context and use CGBitmapContextCreateImage() to create an CGImageRef out of it. 然后使用您的上下文,并使用CGBitmapContextCreateImage()从中创建一个CGImageRef。 Finally, use +[UIImage imageWithCGImage:] to convert that CGImageRef to a UIImage. 最后,使用+ [UIImage imageWithCGImage:]将CGImageRef转换为UIImage。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM