比如项目有“点赞”和“点踩”两个切图,有了图片旋转就可以只导入一个切图。
let dzImg = UIImage(named: "icon_dz")!
let dcImg = flipImage(dzImg)
// UIImage图片旋转
private func flipImage(_ image: UIImage) -> UIImage? {
UIGraphicsBeginImageContextWithOptions(image.size, false, image.scale)
let context = UIGraphicsGetCurrentContext()!
context.translateBy(x: image.size.width / 2, y: image.size.height / 2)
context.rotate(by: CGFloat.pi*2)
context.translateBy(x: -image.size.width / 2, y: -image.size.height / 2)
context.draw(image.cgImage!, in: CGRect(origin: .zero, size: image.size))
let flippedImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return flippedImage
}