2

my image

image download link: https://tbgw.alicdn.com/tps/TB1ZKLuQFXXXXbJXXXXXXXXXXXX-264-57.png_.heic

When use this picture in ios, the alpha channel is not correct.

My code

    NSString *urlString = @"https://tbgw.alicdn.com/tps/TB1ZKLuQFXXXXbJXXXXXXXXXXXX-264-57.png_.heic";
NSURL *url = [NSURL URLWithString:urlString];
NSData *data = [NSData dataWithContentsOfURL:url];
UIImage *image = [UIImage imageWithData:data];

UIImageView *imageView = [[UIImageView alloc] init];
imageView.image = image;
imageView.frame = CGRectMake(10, 100, 390, 100);
[self.view addSubview:imageView];

ios sample

郑振宇
  • 21
  • 3

1 Answers1

1

I have encountered the exact same issue. Looks like if the image was saved in premultiplied alpha mode, it will be loaded in straight alpha mode. After being rendered by UIImageView alpha channel will be broken. A workaround is to reload the image if it is heif:

var fixedImage = image
// Reload image if it is heif
if let utType = image.cgImage?.utType as String?, utType == "public.heic" {
    if let data = image.pngData() {
        if let reloaded = UIImage(data: data) {
            fixedImage = reloaded
        }
    }
}
imageView.image = fixedImage

Even if you reload by encoding it to lossless heif (by setting quality to 1.0), it will work as well.

It is definitely an iOS bug. I don't think it will ever be fixed even if you submit a bug report. You could try and see what happens though.

imxieyi
  • 163
  • 7