2

我正在使用 Quartz 在 iOS 上创建透明图像。但是,此图像在模拟器中以适当的透明度显示,但在设备上显示为白色背景。

这是我为此使用的代码:

+ (UIImage *)clearRect:(CGRect)rect inImage:(UIImage *)image {
    if (UIGraphicsBeginImageContextWithOptions != NULL)
        UIGraphicsBeginImageContextWithOptions([image size], NO, 0.0);
    else
        UIGraphicsBeginImageContext([image size]);
    CGContextRef context = UIGraphicsGetCurrentContext();
    [image drawInRect:CGRectMake(0.0, 0.0, [image size].width, [image size].height)];
    CGContextClearRect(context, rect);
    UIImage *result = UIGraphicsGetImageFromCurrentImageContext();
    NSData *data= UIImagePNGRepresentation(result);
    UIImage *img = [UIImage imageWithData:data];
    UIGraphicsEndImageContext();
    return img;
}

这是我在设备上看到的:

白色图像而不是透明图像

我可能做错了什么?

4

1 回答 1

0

鉴于代码在模拟器而不是您的设备中运行,您可能已经成为下面描述的 cruftmonster 的受害者:

http://weblog.bignerdranch.com/642-beware-the-cruftmonster/

您可能有一些遗留文件已从您的 Xcode 项目中删除,但未从设备上的应用程序包中删除。您最初是否使用不透明图像进行测试?

不确定这是否是解决方案,但它肯定符合您描述的症状。

于 2012-06-08T15:52:00.630 回答