我有以下代码:
@implementation MyImageView
@synthesize image; //image is a UIImage
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
}
return self;
}
-(void) removeFromSuperview
{
self.image = nil;
[super removeFromSuperview];
}
- (void)drawRect:(CGRect)rect
{
// Drawing code
if (self.image)
{
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextClearRect(context, rect);
//the below 2 lines is to prove that the alpha channel of my UIImage is indeed drawn
//CGContextSetFillColorWithColor(context, [UIColor whiteColor].CGColor);
//CGContextFillRect(context, rect);
CGContextDrawImage(context, self.bounds, self.image.CGImage);
}
}
@end
当我运行代码时,我意识到我的视图的背景是黑色的。为了测试我的 UIImage 是否有问题,我使用了 CGContextClearRect(context, rect) 之后注释的 2 行。确实画了一个白色的背景。无论如何我可以删除黑色背景吗?当我初始化 MyImageView 时,我已经将 backgroundColor 设置为 [UIColor clearColor]。
非常感谢任何建议。谢谢