从 UIImage PNG 文件中删除透明度(不是 ALPHA 值)

Dav*_*ruz 2 png uiimageview uiimage ios

我正在使用一个音乐播放器,它从 MP3 的 ID3 资源中获取图像。我遇到过某些艺术品具有透明度。(图像有透明部分)。这导致我的应用程序加载这些图像的速度非常慢。我需要找到一种方法在显示 UIImage 之前删除它的透明度。或者还有其他建议吗?

“将图像的透明部分替换为白色等颜色”

如果需要的话,这是我的代码:

NSURL *url = ad.audioPlayer.url;
AVAsset *asset = [AVAsset assetWithURL:url];
for (AVMetadataItem *metadataItem in asset.commonMetadata) {
    if ([metadataItem.commonKey isEqualToString:@"artwork"]){
        NSDictionary *imageDataDictionary = (NSDictionary *)metadataItem.value;
        NSData *imageData = [imageDataDictionary objectForKey:@"data"];
        UIImage *image = [UIImage imageWithData:imageData];

        // This is the image and the place in code where I want to convert it

       _artworkImageView.image = image;
       _bgImage.image = [image applyDarkEffect];

    }
}
Run Code Online (Sandbox Code Playgroud)

小智 5

我也遇到了示例问题,实际上我不想删除 Alpha 通道,而只是用白色替换透明颜色。我尝试按照从 UIImage 中删除 alpha 通道中的评论中的建议删除 alpha 颜色,但烦人的事情是在这样做之后透明颜色变成黑色,我不知道如何使其变为白色。

最终我只是在图像下用透明部分绘制了一个白色背景,而不触及 Alpha 通道。

代码在这里:

// check if there is alpha channel
CGImageAlphaInfo alpha = CGImageGetAlphaInfo(wholeTemplate.CGImage);
if (alpha == kCGImageAlphaPremultipliedLast || alpha == kCGImageAlphaPremultipliedFirst ||
    alpha == kCGImageAlphaLast || alpha == kCGImageAlphaFirst || alpha == kCGImageAlphaOnly)
{
    // create the context with information from the original image
    CGContextRef bitmapContext = CGBitmapContextCreate(NULL,
                                                       wholeTemplate.size.width,
                                                       wholeTemplate.size.height,
                                                       CGImageGetBitsPerComponent(wholeTemplate.CGImage),
                                                       CGImageGetBytesPerRow(wholeTemplate.CGImage),
                                                       CGImageGetColorSpace(wholeTemplate.CGImage),
                                                       CGImageGetBitmapInfo(wholeTemplate.CGImage)
                                                       );

    // draw white rect as background
    CGContextSetFillColorWithColor(bitmapContext, [UIColor whiteColor].CGColor);
    CGContextFillRect(bitmapContext, CGRectMake(0, 0, wholeTemplate.size.width, wholeTemplate.size.height));

    // draw the image
    CGContextDrawImage(bitmapContext, CGRectMake(0, 0, wholeTemplate.size.width, wholeTemplate.size.height), wholeTemplate.CGImage);
    CGImageRef resultNoTransparency = CGBitmapContextCreateImage(bitmapContext);

    // get the image back
    wholeTemplate = [UIImage imageWithCGImage:resultNoTransparency];

    // do not forget to release..
    CGImageRelease(resultNoAlpha);
    CGContextRelease(bitmapContext);
}
Run Code Online (Sandbox Code Playgroud)