【问题标题】:UIImage from UIColor with alpha来自 UIColor 的带有 alpha 的 UIImage
【发布时间】:2013-03-30 12:53:06
【问题描述】:

我正在尝试从我拥有的这个 UIColor 值创建一个 UIImage。 UIImage 正在创建,但 alpha 值丢失。图像保持不透明。我正在使用以下代码:

@implementation GetImage

+ (UIImage *)imageWithColor:(UIColor *)color andSize:(CGSize)size
{
    UIImage *img = nil;

    CGRect rect = CGRectMake(0, 0, size.width, size.height);
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context,
                               color.CGColor);
    CGContextFillRect(context, rect);

    img = UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();

    return img;
}

@end

我这样称呼:

-(void) setButtonColours{

    UIImage *img1, *img2;

    UIColor *red = [UIColor colorWithRed:205.0/255.0 green:68.0/255.0 blue:51.0/255.0 alpha:1.0],
    *blue = [UIColor colorWithRed:71.0/255.0 green:97.0/255.0 blue:151.0/255.0 alpha:1.0];

    img1 = [GetImage imageWithColor: blue andSize: self.dummyButton.frame.size];

    img2 = [GetImage imageWithColor: red andSize: self.dummyButton.frame.size];

    [self.dummyButton setBackgroundImage: img1 forState:UIControlStateNormal];
    [self.dummyButton setBackgroundImage: img2 forState:UIControlStateSelected];

}

知道我错过了什么吗?

【问题讨论】:

    标签: ios uibutton uiimage uicolor


    【解决方案1】:

    您正在创建一个不透明的图像上下文。

    替换这一行:

    UIGraphicsBeginImageContext(rect.size);
    

    与:

    UIGraphicsBeginImageContextWithOptions(rect.size, NO, 0);
    

    【讨论】:

      【解决方案2】:

      如果有人发现这个问题并且他们正在寻找 Monotouch 实现:

          public class ColorRGBA
              {
                  public float Red=0;
                  public float Green=0;
                  public float Blue=0;
                  public float Alpha=0;
      
                  public ColorRGBA (float red, float green, float blue, float alpha)
                  {
                      Red = red;
                      Green = green;
                      Blue = blue;
                      Alpha = alpha;
                  }
              }
      
      //then from within a custom mt.d element
      
      private ColorRGBA color;
      
          public override UITableViewCell GetCell (UITableView tv)
                      {
                          //create square with color
                          var imageSize = new SizeF(30, 30);
                          var imageSizeRectF = new RectangleF (0, 0, 30, 30);
                          UIGraphics.BeginImageContextWithOptions (imageSize, false, 0);
                          var context = UIGraphics.GetCurrentContext ();
                          context.SetFillColor (color.Red, color.Green, color.Blue, color.Alpha);
                          context.FillRect (imageSizeRectF);
                          var image = UIGraphics.GetImageFromCurrentImageContext ();
                          UIGraphics.EndImageContext ();
      
                          var cell = base.GetCell(tv);
                          cell.Accessory = UITableViewCellAccessory.DisclosureIndicator;
                          cell.ImageView.Image = image;
      
                          return cell;
                      }
      

      【讨论】:

        【解决方案3】:

        对于那些感兴趣的人,这里是在 Swift 4+ 中的简单重写:

        import UIKit
        
        extension UIImage {
          class func colorAsImage(from color:UIColor, for size:CGSize) -> UIImage? {
            var img:UIImage?
            let rect = CGRect(x:0.0, y:0.0, width: size.width, height: size.height)
            UIGraphicsBeginImageContextWithOptions(rect.size, false, 0)
            guard let context = UIGraphicsGetCurrentContext() else { return nil }
            context.setFillColor(color.cgColor)
            context.fill(rect)
            img = UIGraphicsGetImageFromCurrentImageContext()
            UIGraphicsEndImageContext()
            guard let colorImage = img else { return nil }
            return colorImage
          }
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2013-12-23
          • 2015-09-04
          • 1970-01-01
          • 2016-06-11
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多