ios 修改图片png图片rgba通道

这篇博客介绍了如何在iOS中处理PNG图片,将非纯透明的像素改为纯白色,创建一个新的UIImage对象。提供了两个方法,一个用于修改图片的RGBA通道,另一个用于保存图片到缓存。

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

很早之前ios开发用到的图片处理的方法,美工只给了一张png图然后用来用来做一个蒙层的

+(UIImage*)imageToClear:(UIImage*) anImage

{

    CGImageRef imageRef = anImage.CGImage;

    size_t width  = CGImageGetWidth(imageRef);  //获取图片像素的宽

    size_t height = CGImageGetHeight(imageRef); //获取图片像素的高

    size_t bitsPerComponent = CGImageGetBitsPerComponent(imageRef);

    size_t bitsPerPixel = CGImageGetBitsPerPixel(imageRef);

    size_t bytesPerRow = CGImageGetBytesPerRow(imageRef);

    CGColorSpaceRef colorSpace = CGImageGetColorSpace(imageRef);

    CGBitmapInfo bitmapInfo = CGImageGetBitmapInfo(imageRef);

    bool shouldInterpolate = CGImageGetShouldInterpolate(imageRef);

    CGColorRenderingIntent intent = CGImageGetRenderingIntent(imageRef);

    CGDataProviderRef dataProvider = CGImageGetDataProvider(imageRef);

    CFDataRef data = CGDataProviderCopyData(dataProvider);

    UInt8 *buffer = (UInt8*)CFDataGetBytePtr(data);

    

    NSUInteger  x, y;

    for (y = 0; y < height; y++) {

        for (x = 0; x < width; x++) {

            UInt8 *tmp;  //UInt8 8位无符号整型  

            tmp = buffer + y * bytesPerRow + x * 4;  //每个像素4个通道  *4代表下一个像素

            

            UInt8 alpha;

             alpha= *(tmp+3);  //rgba  alpha通道排第四个

            if(alpha>0)    //下面这个 意思是只要图片上的色素都是非纯透明的  都改成纯白色,否则还是纯透明

            {

                *(tmp+3)=255;              

                *(tmp + 0) = 255;

                *(tmp + 1) = 255;

                *(tmp + 2) = 255;

            }else

            {

                *(tmp+3)=0;     

                *(tmp + 0) = 0;

                *(tmp + 1) = 0;

                *(tmp + 2) = 0;

            }

            

        }

    }


//生成图片返回

    CFDataRef effectedData = CFDataCreate(NULL, buffer, CFDataGetLength(data));

    CGDataProviderRef effectedDataProvider = CGDataProviderCreateWithCFData(effectedData);

    CGImageRef effectedCgImage = CGImageCreate(

                                               width, height,

                                               bitsPerComponent, bitsPerPixel, bytesPerRow,

                                               colorSpace, bitmapInfo, effectedDataProvider,

                                               NULL, shouldInterpolate, intent);

    UIImage *effectedImage = [[UIImage alloc] initWithCGImage:effectedCgImage];

    CGImageRelease(effectedCgImage);

    CFRelease(effectedDataProvider);

    CFRelease(effectedData);

    CFRelease(data);

    return effectedImage ;

    

}



注:如果上述处理的图片是网路下载下来的 或者sd_webimage down下来的  处理图片alpha通道之前建议先把图片存到cache 否则可能会崩  

存图片的代码如下:


#define HOME_PATH NSHomeDirectory()

#define LIBRARY_PATH    [HOME_PATH stringByAppendingPathComponent:@"Library/Caches"]


+(UIImage *)saveImageWithImage:(UIImage*)image WithString:(NSString *)nameString

{

    NSData *imageData = UIImagePNGRepresentation(image);

    NSString *fileName = [NSString stringWithFormat:@"%@.png", nameString];

    NSString *picPath=[NSString stringWithFormat:@"%@/%@",LIBRARY_PATH,fileName];

    if([[NSFileManager defaultManager]fileExistsAtPath:picPath])

    {

        [[NSFileManager defaultManager]removeItemAtPath:picPath error:nil];

    }

    [[NSFileManager defaultManager] createFileAtPath:picPath contents:imageData attributes:nil];

    

    NSData* newData = [NSData dataWithContentsOfFile:picPath];

    UIImage *image3=[UIImage imageWithData:newData];

    return image3;


}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值