iPhone: Changing CGImageAlphaInfo of CGImage

后端 未结 2 530
南方客
南方客 2020-12-13 02:45

I have a PNG image that has an unsupported bitmap graphics context pixel format. Whenever I attempt to resize the image, CGBitmapContextCreate() chokes on the u

2条回答
  •  执笔经年
    2020-12-13 03:33

    Yeah, I had problems with 8 bit (indexed) .PNGs. I had to convert it to a more native image to perform graphics operations. I essentially did something like this:

    - (UIImage *) normalize {
    
        CGColorSpaceRef genericColorSpace = CGColorSpaceCreateDeviceRGB();
        CGContextRef thumbBitmapCtxt = CGBitmapContextCreate(NULL, 
                                                             self.size.width, 
                                                             self.size.height, 
                                                             8, (4 * self.size.width), 
                                                             genericColorSpace, 
                                                             kCGImageAlphaPremultipliedFirst);
        CGColorSpaceRelease(genericColorSpace);
        CGContextSetInterpolationQuality(thumbBitmapCtxt, kCGInterpolationDefault);
        CGRect destRect = CGRectMake(0, 0, self.size.width, self.size.height);
        CGContextDrawImage(thumbBitmapCtxt, destRect, self.CGImage);
        CGImageRef tmpThumbImage = CGBitmapContextCreateImage(thumbBitmapCtxt);
        CGContextRelease(thumbBitmapCtxt);    
        UIImage *result = [UIImage imageWithCGImage:tmpThumbImage];
        CGImageRelease(tmpThumbImage);
    
        return result;    
    }
    

提交回复
热议问题