问题描述
默认情况下UIGraphicsImageRenderer
将比例设置为设备的屏幕比例,在iPhone 6s上为2x,在iPhone 6s Plus 3x上,因此,即使您给定尺寸为300的尺寸,它也会根据600或900的尺寸来创建尺寸在哪个设备上使用.如果要确保始终为300,如何设置刻度?
By default UIGraphicsImageRenderer
sets the scale to the device's screen scale, on iPhone 6s it's 2x and iPhone 6s Plus 3x, therefore even though you've given it a size with dimension 300 it's creating it at either 600 or 900 depending on which device is being used. When you want to ensure it's always 300, how do you set the scale?
let outputBounds = CGRect(x: 0, y: 0, width: 300, height: 300)
let renderer = UIGraphicsImageRenderer(bounds: outputBounds)
let image = renderer.image { context in
//...
}
以前,您可以通过此处的最后一个参数设置比例尺:UIGraphicsBeginImageContextWithOptions(bounds.size, false, 1)
Previously you would set the scale via the last parameter here:UIGraphicsBeginImageContextWithOptions(bounds.size, false, 1)
推荐答案
在创建UIGraphicsImageRenderer
时应使用UIGraphicsImageRendererFormat
类.如果要写精确的像素而不是缩放的点,请使用类似以下的内容:
You should use the UIGraphicsImageRendererFormat
class when creating your UIGraphicsImageRenderer
. If you want to write exact pixels rather than scaled points, use something like this:
let format = UIGraphicsImageRendererFormat()
format.scale = 1
let renderer = UIGraphicsImageRenderer(size: yourImageSize, format: format)
let image = renderer.image { ctx in
// etc
}
我保留了 iOS 10代码示例列表,并将在此列表中添加一个.
I'm keeping a list of iOS 10 code examples and will add one based on this.
这篇关于使用UIGraphicsImageRenderer时如何设置比例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!