iosuiimageviewuiimagepng

Remove transparency from a UIImage PNG file (NOT ALPHA VALUE)


I am working with a music player that takes images from the ID3 resources from the MP3. I've encountered certain ArtWorks that are transparent (the images have transparent parts). This is causing my app to load those images very slowly. I need to find a way to remove the transparency of the UIImage before showing it.

"Replace the transparent part of the image with a colour such as white."

Here's my code:

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];

    }
}

Solution

  • Try this...

    //First create a white image that has an equal size with your image.
    
            CGImageRef sourceImage = yourImage.CGImage;
    
            CFDataRef theData;
            theData = CGDataProviderCopyData(CGImageGetDataProvider(sourceImage));
    
            UInt8 *pixelData = (UInt8 *) CFDataGetBytePtr(theData);
    
            int dataLength = CFDataGetLength(theData);
    
            int red = 0;
            int green = 1;
            int blue = 2;
            int alpha = 3;
    
            for (int i = 0; i < (dataLength); i += 4) {
    
            //create white pixels    
    
                int r = 255;
                int b = 255;
                int g = 255;
                int al = 255;
    
    
                pixelData[i + red] = r;
                pixelData[i + blue] = b;
                pixelData[i + green] = g;
                pixelData[i + alpha] = al;
    
    
            }
    
            CGContextRef context;
            context = CGBitmapContextCreate(pixelData,
                                            CGImageGetWidth(sourceImage),
                                            CGImageGetHeight(sourceImage),
                                            8,
                                            CGImageGetBytesPerRow(sourceImage),
                                            CGImageGetColorSpace(sourceImage),
                                            kCGImageAlphaPremultipliedLast);
    
            CGImageRef newCGImage = CGBitmapContextCreateImage(context);
    
            CGContextRelease(context);
            CFRelease(theData);
    
            //Now that you have your image use CIFilter "CISourceOverCompositing".
            //this adds your white image as the background.
    
            UIImageOrientation originalOrientation = origim.imageOrientation;
            data3 = UIImagePNGRepresentation(origim);
            CIImage *result = [CIImage imageWithData:data3];
            CIImage *result2 = [CIImage imageWithCGImage:newCGImage];
    
            CIContext *contextt = [CIContext contextWithOptions:nil];
    
            CIFilter *addBackground = [CIFilter filterWithName:@"CISourceOverCompositing"];
            [addBackground setDefaults];
            [addBackground setValue:result forKey:@"inputImage"];
            [addBackground setValue:result2 forKey:@"inputBackgroundImage"];
            result = [addBackground valueForKey:@"outputImage"];
    
            CGImageRef imgRef = [contextt createCGImage:result fromRect:result.extent];
            image = [[UIImage alloc] initWithCGImage:imgRef scale:1.0 orientation:originalOrientation];
            CGImageRelease(imgRef);