在运行时删除UIImageView中的图像背景

时间:2009-06-13 22:46:30

标签: iphone cocoa-touch interface-builder uiimageview uiimage

我在Interface Builder中将球分配给UIImageView。来自UIImageView的IBOutlet连接到相应的UIViewController。图像具有白色背景。当我将它分配给IB中的UIImageView时,背景是透明的。在IB中,我将UIImageView设置为透明背景和方面填充。

当我在运行时为UIImageView分配图像时:

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

UIImageView方块有一个白色背景,球不显示。意思是四个角落。 IB版本在运行时没有显示白色背景而程序版本有什么区别?

2 个答案:

答案 0 :(得分:6)

除了设置要清除的背景颜色外,还要确保设置self.ball.opaque = NO;。否则,仍会绘制白色背景。我相信你必须设置这两个是否使用IB或Xcode来创建视图 - 但IB可能已经为你设置它们。

答案 1 :(得分:2)

如果您需要以编程方式删除背景,请参阅"Bitmap Images and Image Masks"指南。关于这一点,我没有其他想法...只使用CGImageCreateWithMaskingColors。

如果您的* .PNG(或任何图形资源文件)没有内置透明度(只是白色背景)但IB看起来像透明 - 可能 alpha 属性少于1为你的UIImageView,你看到部分透明的图像。

相关问题