【问题标题】:How to create a black UIImage?如何创建黑色 UIImage?
【发布时间】:2013-04-06 20:58:51
【问题描述】:

我四处张望,但我找不到这样做的方法。我需要创建一个具有一定宽度和高度的黑色 UIImage(宽度和高度会发生变化,所以我不能只创建一个黑盒子然后将其加载到 UIImage 中)。有什么方法可以制作 CGRect 然后将其转换为 UIImage 吗?或者有没有其他方法可以制作一个简单的黑匣子?

【问题讨论】:

标签: ios objective-c cocoa-touch


【解决方案1】:

根据您的情况,您可能只使用UIView 并将其backgroundColor 设置为[UIColor blackColor]。此外,如果图像是纯色的,则您不需要实际上是您想要显示它的尺寸的图像;您可以缩放 1x1 像素图像以填充必要的空间(例如,通过将 UIImageViewcontentMode 设置为 UIViewContentModeScaleToFill)。

话虽如此,看看如何实际生成这样的图像可能会很有启发性:

目标-C

CGSize imageSize = CGSizeMake(64, 64);
UIColor *fillColor = [UIColor blackColor];
UIGraphicsBeginImageContextWithOptions(imageSize, YES, 0);
CGContextRef context = UIGraphicsGetCurrentContext();
[fillColor setFill];
CGContextFillRect(context, CGRectMake(0, 0, imageSize.width, imageSize.height));
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

斯威夫特

let imageSize = CGSize(width: 420, height: 120)
let color: UIColor = .black
UIGraphicsBeginImageContextWithOptions(imageSize, true, 0)
let context = UIGraphicsGetCurrentContext()!
color.setFill()
context.fill(CGRect(x: 0, y: 0, width: imageSize.width, height: imageSize.height))
let image: UIImage = UIGraphicsGetImageFromCurrentImageContext()!
UIGraphicsEndImageContext()

【讨论】:

    【解决方案2】:
    UIGraphicsBeginImageContextWithOptions(CGSizeMake(w,h), NO, 0);
    UIBezierPath* p =
        [UIBezierPath bezierPathWithRect:CGRectMake(0,0,w,h)];
    [[UIColor blackColor] setFill];
    [p fill];
    UIImage* im = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    

    现在im 是图像。

    该代码与我书中的这一部分几乎没有变化:http://www.apeth.com/iOSBook/ch15.html#_graphics_contexts

    【讨论】:

    • 固体。我唯一要改变的是opaque 可以设置为YES,因为我们大概在上下文中绘制了一种不透明的颜色。
    • 实际上,如果您将opaque 设置为YES,我认为您甚至不必绘制黑色填充矩形! :) 一个不透明的图形上下文黑色的。
    • 好收获。我想知道这是否记录在任何地方,或者它只是一个实现怪癖。
    • 我的书中记录了它,所以我希望它不是一个实现怪癖!
    【解决方案3】:

    斯威夫特 3:

    func uiImage(from color:UIColor?, size:CGSize) -> UIImage? {
    
        UIGraphicsBeginImageContextWithOptions(size, true, 0)
        defer {
            UIGraphicsEndImageContext()
        }
    
        let context = UIGraphicsGetCurrentContext()
        color?.setFill()
        context?.fill(CGRect.init(x: 0, y: 0, width: size.width, height: size.height))
        return UIGraphicsGetImageFromCurrentImageContext()
    }
    

    【讨论】:

      【解决方案4】:

      这是一个示例,它通过从 CIImage 创建的 CGImage 创建一个 1920x1080 的黑色 UIImage:

      let frame = CGRect(origin: CGPoint(x: 0, y: 0), size: CGSize(width: 1920, height: 1080))
      let cgImage = CIContext().createCGImage(CIImage(color: .black()), from: frame)!
      let uiImage = UIImage(cgImage: cgImage)
      

      【讨论】:

        【解决方案5】:

        这样

        let image = UIGraphicsImageRenderer(size: bounds.size).image { _ in
              UIColor.black.setFill()
              UIRectFill(bounds)
        }
        

        引用于this WWDC vid

        还有一个更老的函数; UIGraphicsBeginImageContext。但请不要使用它。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2019-08-25
          • 2015-04-07
          • 2011-09-08
          • 2020-01-17
          • 2010-11-02
          • 1970-01-01
          • 1970-01-01
          • 2010-11-24
          相关资源
          最近更新 更多