swift - 使用 UIGraphicsImageRenderer 旋转图像?

标签 swift uikit ios10 image-rendering

UIGraphicsImageRenderer是在 iOS 10 中新引入的。我想知道是否有可能用它旋转 UIImage(任何自定义角度)。我知道有 classic way使用 CGContextRotateCTM

最佳答案

您可以设置 UIGraphicsImageRenderer 来创建图像并调用 UIGraphicsGetCurrentContext() 并旋转上下文

let renderer = UIGraphicsImageRenderer(size:sizeOfImage)
let image = renderer.image(actions: { _ in
    let context = UIGraphicsGetCurrentContext()

    context?.translateBy(x: orgin.x, y: orgin.y)
    context?.rotate(by: angle)
    context?.draw(image.cgImage!, in: CGRect(origin: CGPoint(x: -orgin.x,y: -orgin.y), size: size))
}
return image

关于swift - 使用 UIGraphicsImageRenderer 旋转图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39089148/

相关文章:

ios - 来自 Storyboard 的 View 不会出现在 Xcode Preview 中

swift - 使用 UIBezierPath 的带有圆角的 TableView 页眉和页脚

ios - 如何在 tableview 中为图像设置动画以同时扩展和打开另一个 View Controller ?

ios - 在 IOS 10 上使用设备 CAMERA 会导致应用程序终止

swift - 将 UIButtons 添加到折线图中的数据点,在 X 轴上绘制日期和时间

ios - 在 Swift 中以编程方式添加约束不起作用

ios - 在 UITableViewCell 中委托(delegate) UICollectionView

swift - 如何用动画改变单元格编辑风格

ios - 无法在 iOS 10 中使用分布式配置文件验证企业应用程序

Swift 可选协议(protocol)要求