简体   繁体   English

为什么不是我的UIGraphicsImageRenderer绘图(iOS 10)?

[英]Why isn't my UIGraphicsImageRenderer drawing (iOS 10)?

UIGraphicsImageRenderer is the new iOS 10 way to create an image context and draw into it. UIGraphicsImageRenderer是创建图像上下文并绘制到其中的新iOS 10方式。 I'm trying to use UIGraphicsImageRenderer to draw a simple image: 我正在尝试使用UIGraphicsImageRenderer绘制一个简单的图像:

let size = CGSize(width:100,height:100)
let f = UIGraphicsImageRendererFormat()
f.opaque = false
let r = UIGraphicsImageRenderer(size:size, format: f)
let im = r.image { _ in
    let p = UIBezierPath(ovalIn: CGRect(origin: CGPoint.zero, size: size))
    UIColor.blue().setFill()
    p.fill()
}
let iv = UIImageView(image:im)
iv.frame.origin = CGPoint(x: 200, y: 200)
self.view.addSubview(iv)

I expect to see a blue filled circle. 我希望看到一个蓝色的圆圈。 But I don't see anything. 但我没有看到任何东西。 What am I doing wrong? 我究竟做错了什么?

PS I'm using Xcode 8 Seed 3, if it matters. PS我正在使用Xcode 8 Seed 3,如果重要的话。

The problem is that that's not how you make a UIGraphicsImageRendererFormat. 问题是,这不是你如何制作UIGraphicsImageRendererFormat。 The documentation is a bit unclear on this point, but in fact you should be saying this: 关于这一点,文档有点不清楚,但事实上你应该这样说:

let size = CGSize(width:100,height:100)
let f = UIGraphicsImageRendererFormat.default() // *
f.opaque = false
let r = UIGraphicsImageRenderer(size:size, format: f)
let im = r.image { _ in
    let p = UIBezierPath(ovalIn: CGRect(origin: CGPoint.zero, size: size))
    UIColor.blue().setFill()
    p.fill()
}
let iv = UIImageView(image:im)
iv.frame.origin = CGPoint(x: 200, y: 200)
self.view.addSubview(iv)

Your code will then work perfectly. 您的代码将完美运行。

The problem with the previous code is that UIGraphicsImageRendererFormat() creates a completely unconfigured UIGraphicsImageRendererFormat, which is thus pretty much useless (unless you set all of its properties yourself). 上一个代码的问题是UIGraphicsImageRendererFormat()创建了一个完全未配置的 UIGraphicsImageRendererFormat,因此几乎没用(除非你自己设置它的所有属性)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM