Removing image background in UIImageView at runtime

I have a ball assigned by UIImageView in Interface Builder. The IBOutlet of the UIImageView is connected to the corresponding UIViewController. Image has a white background. When I assign it to a UIImageView in IB, the background is transparent. In IB, I have UIImageView set to a transparent background and fill aspect.

When I assign a UIImageView image at runtime:

self.ball.image = ballImage; //ballImage is a UIImage self.ball.backgroundColor = [UIColor clearColor]; self.ball.contentMode = UIViewContentModeScaleAspectFill; 

UIImageView square has a white background where the ball is not displayed. The value of all four angles. What is the difference that the IB version does not show a white background at run time and the software version?

+4
source share
2 answers

Make sure you set self.ball.opaque = NO; in addition to adjusting the background color for cleaning. Otherwise, the white background will still be drawn. I believe that you should install both of them: whether you use IB or Xcode to create the presentation, but IB can install them for you.

+6
source

If you need to remove the background programmatically, you can refer to "Raster images and image masks . " I have no other idea on this ... just using CGImageCreateWithMaskingColors.

If your * .PNG (or any graphic resource file) does not have built-in transparency (only on a white background), but it looks transparent with IB, there may be an alpha property less than 1 for your UIImageView, and you see a partially transparent image.

+2
source

Source: https://habr.com/ru/post/1286180/


All Articles