RGB转化成bitmap格式

这里贴一个我写的函数:

-(UIImage*)CreateBitmap:(NSInteger)nPage
{
    // 显示
    NSInteger rt = [self displayPage:nPage];
    if (rt != 0){
        NSLog(@"LoadPageToCache PDFDataController DisplayPage 执行失败!");
    }
    // 取得页面位图
    unsigned char* rgba = [mPDFKit GetPageBitmapData];
    NSAssert(rgba != NULL, @"rgba object is missing !");
    NSInteger w = [mPDFKit GetPageBitmapWidth];
    NSInteger h = [mPDFKit GetPageBitmapHeight];
    // 构造图像
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    CGContextRef bitmapContext = CGBitmapContextCreate(rgba,w,h,8,4*w,colorSpace,kCGImageAlphaNoneSkipLast);
    if (!bitmapContext) {
        NSLog(@"位图上下文为空!");
        return nil;
    }
    NSLog(@" 原始数据大小为:%d",w*h*4);
    CGImageRef cgRef = CGBitmapContextCreateImage(bitmapContext);
    UIImage* image = [UIImage imageWithCGImage: cgRef];
    
    NSData*imageData = UIImageJPEGRepresentation(image, 0.1);// 质量为原来的1/10
    NSInteger size = [imageData length];
    NSLog(@" 压缩后数据大小为:%d",size);
    UIImage *aimage =[UIImage imageWithData: imageData];
    if (rgba != NULL) {
        //free(rgba);// 释放原始数据
    }
    CGColorSpaceRelease( colorSpace );
    CGImageRelease(cgRef);
    CGContextRelease(bitmapContext);
    return aimage;
}
以下为转载的内容:

ask:

I have a buffer of RGB unsigned char that I would like converted into a bitmap file, does anyone know how?

My RGB float is of the following format

R [(0,0)], G[(0,0)], B[(0,0)],R [(0,1)], G[(0,1)], B[(0,1)], R [(0,2)], G[(0,2)], B[(0,2)] .....

The values for each data unit ranges from 0 to 255. anyone has any ideas how I can go about making this conversion?


answer:

You can use CGBitmapContextCreate to make a bitmap context from your raw data. Then you can create a CGImageRef from the bitmap context and save it. Unfortunately CGBitmapContextCreate is a little picky about the format of the data. It does not support 24-bit RGB data. The loop at the beginning swizzles the rgb data to rgba with an alpha value of zero at the end. You have to include and link with ApplicationServices framework.

char* rgba = (char*)malloc(width*height*4);
for(int i=0; i < width*height; ++i) {
    rgba[4*i] = myBuffer[3*i];
    rgba[4*i+1] = myBuffer[3*i+1];
    rgba[4*i+2] = myBuffer[3*i+2];
    rgba[4*i+3] = 0;
}
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef bitmapContext = CGBitmapContextCreate(
    rgba,
    width,
    height,
, // bitsPerComponent
*width, // bytesPerRow
    colorSpace,
    kCGImageAlphaNoneSkipLast);

CFRelease(colorSpace);

CGImageRef cgImage = CGBitmapContextCreateImage(bitmapContext);
CFURLRef url = CFURLCreateWithFileSystemPath(kCFAllocatorDefault, CFSTR("image.png"), kCFURLPOSIXPathStyle, false);

CFStringRef type = kUTTypePNG; // or kUTTypeBMP if you like
CGImageDestinationRef dest = CGImageDestinationCreateWithURL(url, type, 1, 0);

CGImageDestinationAddImage(dest, cgImage, 0);

CFRelease(cgImage);
CFRelease(bitmapContext);
CGImageDestinationFinalize(dest);
free(rgba);


int width = 11;
int height = 8;

Byte r[8][11]={
        {000,000,255,000,000,000,000,000,255,000,000},
        {000,000,000,255,000,000,000,255,000,000,000},  
        {000,000,255,255,255,255,255,255,255,000,000},
        {000,255,255,255,255,255,255,255,255,255,000},
        {255,255,255,255,255,255,255,255,255,255,255},
        {255,000,255,255,255,255,255,255,255,000,255},
        {255,000,255,000,000,000,000,000,255,000,255},
        {000,000,000,255,255,000,255,255,000,000,000}};

Byte g[8][11]={
        {000,000,255,000,000,000,000,000,255,000,000},
        {000,000,000,255,000,000,000,255,000,000,000},  
        {000,000,255,255,255,255,255,255,255,000,000},
        {000,255,255,000,255,255,255,000,255,255,000},
        {255,255,255,255,255,255,255,255,255,255,255},
        {255,000,255,255,255,255,255,255,255,000,255},
        {255,000,255,000,000,000,000,000,255,000,255},
        {000,000,000,255,255,000,255,255,000,000,000}};

Byte b[8][11]={
        {000,000,255,000,000,000,000,000,255,000,000},
        {000,000,000,255,000,000,000,255,000,000,000},  
        {000,000,255,255,255,255,255,255,255,000,000},
        {000,255,255,000,255,255,255,000,255,255,000},
        {255,255,255,255,255,255,255,255,255,255,255},
        {255,000,255,255,255,255,255,255,255,000,255},
        {255,000,255,000,000,000,000,000,255,000,255},
        {000,000,000,255,255,000,255,255,000,000,000}};

char* rgba = (char*)malloc(width*height*4);
int offset=0;
for(int i=0; i < height; ++i) 
{
        for (int j=0; j < width; j++) 
        {
                rgba[4*offset]   = r[i][j];
                rgba[4*offset+1] = g[i][j];
                rgba[4*offset+2] = b[i][j];
                rgba[4*offset+3] = 0;
                offset ++;
        }
}


CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef bitmapContext = CGBitmapContextCreate(
    rgba,
    width,
    height,
, // bitsPerComponent
*width, // bytesPerRow
    colorSpace,
    kCGImageAlphaNoneSkipLast                    
);

CFRelease(colorSpace);

CGImageRef cgImage = CGBitmapContextCreateImage(bitmapContext);

free(rgba);

UIImage *newUIImage = [UIImage imageWithCGImage:cgImage];

UIImageView *iv = [[UIImageView alloc] initWithFrame:CGRectMake(10, 10, 11,8)];

[iv setImage:newUIImage];

Then,  addSubview:iv  to get the image into your view and, of course, do the obligatory  [releases] to keep a clean house.

CGContextRef ctx = CGBitmapContextCreate(m_PixelBuf,  

                                                 CGImageGetWidth(grayImage),  

                                                 CGImageGetHeight(grayImage),  

                                                 CGImageGetBitsPerComponent(grayImage),

                                                 CGImageGetBytesPerRow(grayImage),  

                                                 CGImageGetColorSpace(grayImage), 

                                                 CGImageGetBitmapInfo(grayImage)

                                                 );

Creating Bitmap Contexts

Getting Information About Bitmap Contexts

These functions return the values of attributes specified when a bitmap context is created.

Functions

CGBitmapContextCreate

Creates a bitmap graphics context.

CGContextRef CGBitmapContextCreate (
 void *data,
 size_t width,
 size_t height,
 size_t bitsPerComponent,
 size_t bytesPerRow,
 CGColorSpaceRef colorspace,
 CGBitmapInfo bitmapInfo
);


UIImage *dotline = [UIImage imageNamed:@"XXX.png"];
    UIGraphicsBeginImageContext(CGSizeMake(self.view.frame.size.width-4, 1));
    [dotline drawAsPatternInRect:CGRectMake(0, 0, self.view.frame.size.width-4, 1)];
    return UIGraphicsGetImageFromCurrentImageContext();

原文:

http://www.cnblogs.com/pengyingh/articles/2416332.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值