使用 UIGraphicsImageRenderer 时如何设置比例

How to set the scale when using UIGraphicsImageRenderer(使用 UIGraphicsImageRenderer 时如何设置比例)
本文介绍了使用 UIGraphicsImageRenderer 时如何设置比例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

默认情况下 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 时如何设置比例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Why local notification is not firing for UNCalendarNotificationTrigger(为什么没有为UNCalendarNotificationTrigger触发本地通知)
iOS VoiceOver functionality changes with Bundle Identifier(IOS画外音功能随捆绑包标识符而变化)
tabbar middle tab out of tabbar corner(选项卡栏中间的选项卡角外)
Pushing UIViewController above UITabBar(将UIView控制器推送到UITabBar上方)
How can I sync two flatList scroll position in react native(如何在本机Reaction中同步两个平面列表滚动位置)
Get an event when UIBarButtonItem menu is displayed(显示UIBarButtonItem菜单时获取事件)