iOS 位图数据及apha处理

本文介绍如何使用C和Objective-C创建灰度图与RGBA图像的过程。通过详细代码示例展示了如何分配内存、设置图像属性及构造不同色彩空间的位图上下文。此外,还提供了一个将灰度数据转换为RGBA格式并加入透明通道的具体实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 


//buffer 的值 是根据需要处理过后的位图数据,如果是灰度图数据,每个像素是1个字节;如果是rgba图数据,每个像素是四个字节,要把apha数据组装到每组数据的末尾,

unsigned char* buff = new unsigned char[128*512*4];

    

    

    memset(buff,255,128*512);//赋值buff 255 即全白数据。

    

    

    NSInteger w = 128 ;//w,h根据位图大小

    NSInteger h = 512 ;

    NSUInteger bytesPerPixel = 1;//如果是灰度图值为1,rgb则为4.

    NSUInteger bytesPerRow = bytesPerPixel * w;

    NSUInteger bitsPerComponent = 8;//32位像素的图像,每个单元组件(理解为每个小色块)8位,共4个字节

    

    //CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceGray();

   

    CGContextRef bitmapContext = CGBitmapContextCreate(buff,//渲染的内存块的首地址

                                                       w,h,bitsPerComponent,bytesPerRow,

                                                       //CGColorSpaceCreateDeviceRGB(),

                                                       CGColorSpaceCreateDeviceGray(),

                                                       kCGImageAlphaNone);

    

    CGImageRef cgRef;

    UIImage* img;

    

    if (!bitmapContext) {

        CGContextRelease(bitmapContext);

       // CGColorSpaceRelease( colorSpace );

        NSLog(@"位图上下文为空!");

        return ;

    }

    cgRef = CGBitmapContextCreateImage(bitmapContext);

    

    img = [UIImage imageWithCGImage: cgRef];

    self.scanImg.image = img;

    

    CGImageRelease(cgRef);

    CGContextRelease(bitmapContext);

   // CGColorSpaceRelease( colorSpace );

    

    delete[] buff;

    

下面贴一个RGBA数据加alpha处理实例;

    

-(UIImage*)otherMethod:(Byte*)dataPtr{

    NSInteger w = 519;

    NSInteger h = 128;

    long size = w * h;

    

    unsigned char* rgba = (unsigned char*)malloc(size*4);

    

    for(int i = 0; i < size; i++) {

        rgba[4*i]   = dataPtr[3*i];

        rgba[4*i+1] = dataPtr[3*i+1];

        rgba[4*i+2] = dataPtr[3*i+2];

        rgba[4*i+3] = 0;

    }

    // 构造图像

    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();

    CGContextRef bitmapContext = CGBitmapContextCreate(rgba,w,h,8,4*w,colorSpace,kCGImageAlphaPremultipliedLast);

    if (!bitmapContext) {

        NSLog(@"位图上下文为空!");

        return nil;

    }

    CGImageRef cgRef = CGBitmapContextCreateImage(bitmapContext);

    UIImage* image = [UIImage imageWithCGImage: cgRef];

    

    if (rgba != NULL) {

        free(rgba);// 释放原始数据

    }

    CGColorSpaceRelease( colorSpace );

    CGImageRelease(cgRef);

    CGContextRelease(bitmapContext);

    self.scanImg.image = image;

    return image;

}


- (IBAction)clickFreezing:(id)sender {

    unsigned char* buff = new unsigned char[128*512*4];

    memset(buff,255,128*512*4);//第二个参数修改颜色值1-255

    [self otherMethod:buff];

   

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值