如何在Objective-C中将字节数组转换为图像 [英] How to convert bytearray to image in Objective-C
问题描述
情况就是这样
我有一个指向BMP图像数据的无符号char指针.
在我用指针循环后,我实现了一个字节数组,该数组包含int值0-255
This is the Case
i have an unsigned char pointer to BMP image data
after i loop with the pointer i achieved a byte array contain int values 0 - 255
我想要的是将数组中的此值转换为BMP图像 在UIImage中显示它.
What i want is convert this values in the array to BMP image to display it in UIImage.
**图像为灰度
推荐答案
此代码段来自以下 Github中的项目站点
This code snippet is from this blog, I recommend you take a look there and the project site in Github
还请注意,此类方法适用于RGB8图像,因此您需要在
bitsPerPixel
(灰度应该为8),bytesPerRow
(1 *宽度),bufferLength
(删除* 4)并改为使用CGColorCreateGenericGray
创建colorSpaceRef
.
Also Note that this class method works for RGB8 Images, so you will need to make changes in
bitsPerPixel
(it should be 8 for grayscale), bytesPerRow
(1 * width), bufferLength
(remove * 4) and create colorSpaceRef
using CGColorCreateGenericGray
instead.
我还注意到,使用CGColorCreateGenericGray
创建色彩空间时假设您的数组具有alpha信息.因此,也许您应该为每个像素添加一个alpha字节,以使其正常工作.
Also I've noticed that creating the color space with CGColorCreateGenericGray
assumes that your array has alpha info. So maybe you should add an alpha byte for each pixel to make it work properly.
+ (UIImage *) convertBitmapRGBA8ToUIImage:(unsigned char *) buffer
withWidth:(int) width
withHeight:(int) height {
size_t bufferLength = width * height * 4;
CGDataProviderRef provider = CGDataProviderCreateWithData(NULL, buffer, bufferLength, NULL);
size_t bitsPerComponent = 8;
size_t bitsPerPixel = 32;
size_t bytesPerRow = 4 * width;
CGColorSpaceRef colorSpaceRef = CGColorSpaceCreateDeviceRGB();
if(colorSpaceRef == NULL) {
NSLog(@"Error allocating color space");
CGDataProviderRelease(provider);
return nil;
}
CGBitmapInfo bitmapInfo = kCGBitmapByteOrderDefault | kCGImageAlphaPremultipliedLast;
CGColorRenderingIntent renderingIntent = kCGRenderingIntentDefault;
CGImageRef iref = CGImageCreate(width,
height,
bitsPerComponent,
bitsPerPixel,
bytesPerRow,
colorSpaceRef,
bitmapInfo,
provider, // data provider
NULL, // decode
YES, // should interpolate
renderingIntent);
uint32_t* pixels = (uint32_t*)malloc(bufferLength);
if(pixels == NULL) {
NSLog(@"Error: Memory not allocated for bitmap");
CGDataProviderRelease(provider);
CGColorSpaceRelease(colorSpaceRef);
CGImageRelease(iref);
return nil;
}
CGContextRef context = CGBitmapContextCreate(pixels,
width,
height,
bitsPerComponent,
bytesPerRow,
colorSpaceRef,
bitmapInfo);
if(context == NULL) {
NSLog(@"Error context not created");
free(pixels);
}
UIImage *image = nil;
if(context) {
CGContextDrawImage(context, CGRectMake(0.0f, 0.0f, width, height), iref);
CGImageRef imageRef = CGBitmapContextCreateImage(context);
// Support both iPad 3.2 and iPhone 4 Retina displays with the correct scale
if([UIImage respondsToSelector:@selector(imageWithCGImage:scale:orientation:)]) {
float scale = [[UIScreen mainScreen] scale];
image = [UIImage imageWithCGImage:imageRef scale:scale orientation:UIImageOrientationUp];
} else {
image = [UIImage imageWithCGImage:imageRef];
}
CGImageRelease(imageRef);
CGContextRelease(context);
}
CGColorSpaceRelease(colorSpaceRef);
CGImageRelease(iref);
CGDataProviderRelease(provider);
if(pixels) {
free(pixels);
}
return image;
}
这篇关于如何在Objective-C中将字节数组转换为图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!