【问题标题】:UIImage not transparent png fileUIImage不透明的png文件
【发布时间】:2014-07-05 09:21:46
【问题描述】:

我有一个简单的 png 文件。该图像是一个带有蓝色边框和透明填充的矩形。

我将它添加到我的 UIButton 中,如下所示:

[myButton setImage:[UIImage imageNamed:@"CustomisationsBorder.png"] forState:UIControlStateSelected];

为什么当我将它添加到我的 UIButton 时没有保留透明度?

我每页有几个按钮,我不想创建图像的正常状态和图像的选定状态..

【问题讨论】:

  • 因为UIButton也有自己的alpha。
  • 即使我调整了 alpha,它也会导致整个按钮褪色。
  • 是的,当你减少按钮的 alpha 时,整个按钮会褪色。
  • 检查这些:1) uibutton 的类型应该是 UIButtonTypeCustom 2) 进行项目/清理以确保将正确的图像发送到测试设备。也尝试添加状态 UIControlStateNormal 以检查正常按钮状态的一切是否正常。
  • 我实际上已经对按钮进行了子类化,但现在使用标准 UIButton 以便我可以更改类型。那也没有用。清理后,我设置了 UIButton [myButton setImage:[UIImage imageNamed:@"NormalState.png"] forState:UIControlStateNormal]; 的正常状态

标签: ios objective-c uibutton uiimage transparency


【解决方案1】:

由于 iOS7 按钮将它们的图像渲染为蒙版,并使用它们的 tintColor 着色。所有非完全透明的像素都将在 Color 中渲染。因此,请确保您的内部填充完全透明或使用:

[myButton setBackgroundImage:image forState:UIControlStateSelected]

或确保渲染原始图像

[[UIImage imageNamed:@"CustomisationsBorder.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]

【讨论】:

  • 我的图像填充是透明的。当我查看图像并在 illustrator 中设置透明度时,我可以在 xcode 中看到透明度。但是当它在模拟器上时,它被填充为白色。 UIImageRenderingModeAlwaysOriginal 不起作用
【解决方案2】:

我现在选择了这个解决方案。此代码可在此处和某处的随机博客上找到。

- (UIImage*)imageWithBorderFromImage:(UIImage*)source {
    CGSize size = [source size];
    UIGraphicsBeginImageContext(size);
    CGRect rect = CGRectMake(0, 0, size.width, size.height);
    [source drawInRect:rect blendMode:kCGBlendModeNormal alpha:1.0];

    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetRGBStrokeColor(context, 0.0, 0.0, 1.0, 1.0);
    CGContextSetLineWidth(context, 5.0);
    CGContextStrokeRect(context, rect);
    UIImage *testImg =  UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return testImg;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-10-15
    • 2015-04-17
    • 2012-05-29
    • 2020-04-13
    • 1970-01-01
    • 1970-01-01
    • 2011-06-22
    相关资源
    最近更新 更多