【问题标题】:IOS UIImageView displays with black backgroundIOS UIImageView 显示为黑色背景
【发布时间】:2014-04-29 03:34:12
【问题描述】:

所以我有了这个缩放UIImages 的函数,我用它来初始化一个带有图像的UIImageView。我的问题是,当图像显示时,它周围总是有一个黑色矩形,尽管图像是背景中完全透明的 .png。

这里是缩放方法以及UIImageView的初始化:

- (UIImage *)imageWithImage:(UIImage *)image scaledToSize:(CGSize)newSize {
if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)]) {
    if ([[UIScreen mainScreen] scale] == 2.0) {
        UIGraphicsBeginImageContextWithOptions(newSize, YES, 2.0);
    } else {
        UIGraphicsBeginImageContext(newSize);
    }
} else {
    UIGraphicsBeginImageContext(newSize);
}
[image drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}

- (void)addMass:(Mass *)mass{
UIImageView *image = [[UIImageView alloc]initWithImage:[self imageWithImage:[UIImage imageNamed:@"circle.png"] scaledToSize:CGSizeMake(mass.mass, mass.mass)]];
[image setBackgroundColor:[[UIColor alloc]initWithRed:0 green:0 blue:0 alpha:1]];
image.center = [mass position];
[image setBackgroundColor:[UIColor clearColor]];
[massImages addObject:image];
[self.view addSubview:image];
}

如您所见,我使用[image setBackgroundColor:[UIColor clearColor]]; 尝试将UIImageView 的背景颜色设置为清除,但这似乎不起作用。

【问题讨论】:

  • 可以展示massImages的声明吗?

标签: ios iphone objective-c uiimageview


【解决方案1】:

你需要做的就是

UIGraphicsBeginImageContextWithOptions( newSize, NO, 0 );

并且比例因子会自动设置,结果图像中未触及的像素将是透明的。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-20
    • 1970-01-01
    • 2017-08-30
    • 2010-12-02
    • 2021-11-22
    相关资源
    最近更新 更多