这是案件
我有一个指向BMP图像数据的无符号字符指针
在我用指针循环后我实现了一个字节数组包含int值0 - 255
我想要的是将数组中的这些值转换为BMP图像 在UIImage中显示它。
**图像为灰度
答案 0 :(得分:3)
我会想象这样的事情。未经测试,所以准备调整:)
UIImage *yourImage = [UIImage imageWithData: [NSData dataWithBytes: yourCharPointer length : sizeof(yourCharPointer)]];
答案 1 :(得分:3)
此代码段来自此blog,我建议您查看一下project site in Github
另请注意,此类方法适用于RGB8图像,因此您需要进行更改
bitsPerPixel
(灰度应为8),bytesPerRow
(1 *宽度),bufferLength
(删除* 4)并使用colorSpaceRef
创建CGColorCreateGenericGray
。
另外我注意到使用CGColorCreateGenericGray
创建颜色空间假设您的数组具有alpha信息。所以也许你应该为每个像素添加一个字母字节,以使其正常工作。
+ (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;
}
答案 2 :(得分:1)
由于接受的答案很长,我已经写了另一个解决方案:
- (UIImage*) imageFromArray:(const char*)pixelArray width:(int)width height:(int)height {
int imageSizeInPixels = width * height;
int bytesPerPixel = 2; // 1 byte for brightness, 1 byte for alpha
unsigned char *pixels = (unsigned char *)malloc(imageSizeInPixels * bytesPerPixel);
memset(pixels, 255, imageSizeInPixels * bytesPerPixel); // setting alpha values to 255
for (int i = 0; i < imageSizeInPixels; i++) {
pixels[i * 2] = pixelArray[i]; // writing array of bytes as image brightnesses
}
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceGray();
CGDataProviderRef provider = CGDataProviderCreateWithData(NULL, pixels, imageSizeInPixels * bytesPerPixel, NULL);
CGImageRef cgImage = CGImageCreate(width, height, 8, 8 * bytesPerPixel, width * bytesPerPixel, colorSpace, kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big, provider, NULL, false, kCGRenderingIntentDefault);
UIImage *image = [UIImage imageWithCGImage:cgImage];
return image;
}