如何从unsigned char创建图像*

时间:2011-08-29 04:13:06

标签: iphone ios image

我正在尝试使用unsigned char *中的原始数据创建一个新图像。我从存储的数据中获得每像素位数。 我尝试将原始数据转换为NSData,但结果图像为零,

  

unsigned char * bitmap = myData;

     

NSUInteger myImageDataLength = strlen((const char *)bitmap);

     

NSData * d = [NSData dataWithBytes:位图长度:myImageDataLength];

     

imgView.image = [UIImage imageWithData:d];

然后我尝试使用上下文创建图像,如下所示:

CGColorSpaceRef colorspace = CGColorSpaceCreateDeviceRGB();
int width = mWidth;
int height = mHeight;
size_t bpp = mBpp;
size_t bpc = 8;
size_t bpr = (width * 4);
CGContextRef context = CGBitmapContextCreate(bitmap, width, height, 8, bpr, colorspace, kCGImageAlphaNone);
CGImageRef cgImage = nil;
if (context != nil) {
    cgImage = CGBitmapContextCreateImage (context);
    CGContextRelease(context);
}
CGColorSpaceRelease(colorspace);
UIImage *newImage = [UIImage imageWithCGImage:cgImage];

我收到一条错误消息 的 <Error>: CGBitmapContextCreate: unsupported parameter combination: 8 integer bits/component; 24 bits/pixel; 3-component color space; kCGImageAlphaNone; 64 bytes/row.

我尝试通过设置多个值来解决它,但无法找到任何结果。我在网上搜索了这个解决方案,但所有其他人都是从现有图像创建一个图像,所以他们得到每个组件的比特值和所有...我怎样才能从这个数据创建图像???

原始图片:

enter image description here

结果图片:

enter image description here

应该扭曲产生的图像。 (与fatBooth和其他应用程序一样扭曲)

2 个答案:

答案 0 :(得分:5)

以下代码从原始像素数据创建UIImage实例,假设您有:

  • pixelData :原始像素数据
  • imageWidth imageHeight :图片尺寸
  • scanWidth :每条扫描线的字节数
  • bytesPerPixel :每个像素使用的字节数,通常为4

结果UIImage已分配给变量uiImage

CGDataProviderRef provider = CGDataProviderCreateWithData(
    NULL, 
    pixelData, 
    imageHeight * scanWidth, 
    NULL);

CGColorSpaceRef colorSpaceRef = CGColorSpaceCreateDeviceRGB();
CGBitmapInfo bitmapInfo = kCGBitmapByteOrderDefault;
CGColorRenderingIntent renderingIntent = kCGRenderingIntentDefault;

CGImageRef imageRef = CGImageCreate(imageWidth,
    imageHeight,
    8,
    bytesPerPixel * 8,
    scanWidth,
    colorSpaceRef,
    bitmapInfo,
    provider,
    NULL,
    NO,
    renderingIntent);

UIImage *uiImage = [UIImage imageWithCGImage:imageRef];
CGDataProviderRelease(provider);
CGColorSpaceRelease(colorSpaceRef);
CGImageRelease(imageRef);

答案 1 :(得分:0)

试试这个: -

CGRect screenBounds = [[UIScreen mainScreen] bounds];
GLuint *buffer;
//point the buffer to memory location code that here accordingly
int backingWidth = screenBounds.size.width;
int backingHeight =screenBounds.size.height;    
NSInteger myDataLength = backingWidth * backingHeight * 4;


CGDataProviderRef provider = CGDataProviderCreateWithData(NULL, buffer, myDataLength, releaseScreenshotData);
const int bitsPerComponent = 8;
const int bitsPerPixel = 4 * bitsPerComponent;
const int bytesPerRow = 4 * backingWidth;

CGColorSpaceRef colorSpaceRef = CGColorSpaceCreateDeviceRGB();
CGBitmapInfo bitmapInfo = kCGBitmapByteOrderDefault;
CGColorRenderingIntent renderingIntent = kCGRenderingIntentDefault;
CGImageRef imageRef = CGImageCreate(backingWidth,backingHeight, bitsPerComponent, bitsPerPixel, bytesPerRow, colorSpaceRef, bitmapInfo, provider, NULL, NO, renderingIntent);
CGColorSpaceRelease(colorSpaceRef);
CGDataProviderRelease(provider);

 UIImage *myImage = [UIImage imageWithCGImage:imageRef];
CGImageRelease(imageRef);
 [uiimageviewobject setImage:myImage];
 [self addSubview:uiimageviewobject];