我正在尝试使用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.
我尝试通过设置多个值来解决它,但无法找到任何结果。我在网上搜索了这个解决方案,但所有其他人都是从现有图像创建一个图像,所以他们得到每个组件的比特值和所有...我怎样才能从这个数据创建图像???
原始图片:
结果图片:
应该扭曲产生的图像。 (与fatBooth和其他应用程序一样扭曲)
答案 0 :(得分:5)
以下代码从原始像素数据创建UIImage
实例,假设您有:
结果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];