ios从rgb数据生成UIImage

本文介绍如何使用C语言创建位图缓冲区,并利用Core Graphics框架将位图转换成CGImage,最后通过UIImageView在iOS应用中显示该图像。

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

   int width = 100;

    int height = 100;

    int bytes_per_pix = 4;

    

    Byte *rgbbuffer = malloc(width*height*bytes_per_pix);

    for(int i=0;i<width*height*bytes_per_pix;i+=bytes_per_pix)

    {

        rgbbuffer[i+3] = 200;//这个是alfa值,因为是小字节序所以ARGB的内存储存方式为BGRA

        rgbbuffer[i+1] = 100;

    }

    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();

    CGContextRef newContext = CGBitmapContextCreate(rgbbuffer,

                                                    width, height, 8,

                                                    width * bytes_per_pix,

                                                    colorSpace, kCGBitmapByteOrder32Little | kCGImageAlphaPremultipliedFirst);

    CGImageRef frame = CGBitmapContextCreateImage(newContext);

    self.imageview.image = [UIImage imageWithCGImage:frame];

    CGImageRelease(frame);

    CGContextRelease(newContext);

    CGColorSpaceRelease(colorSpace);

free(rgbbuffer);

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值