Last active
November 28, 2016 12:02
-
-
Save SongJiaqiang/dcf0ebc57b8e0fb8f714a5dc02833cac to your computer and use it in GitHub Desktop.
flip UIImage
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 直接翻转UIImageView | |
func flipImageView(imageView: UIView) { | |
imageView.transform = CGAffineTransform(scaleX: -1, y: 1) | |
} | |
// 修改imageOrientation属性, 这个方法只适用于UIKit中显示的image | |
func flipImage(originImage: UIImage) -> UImage { | |
let cgImage = originImage.cgImage! | |
let flipedImage = UIImage(cgImage: originImage, | |
scale: originImage.scale, | |
orientation: UIImageOrientation.upMirrored) | |
return flipedImage | |
} | |
// 如果需要处理图片数据,需要使用Quartz 2D重绘 | |
- (UIImage *)horizontalFlip { | |
CGRect newFrame = CGRectMake(0, 0, self.size.width, self.size.height); | |
UIGraphicsBeginImageContextWithOptions(self.size, NO, self.scale); | |
CGContextRef context = UIGraphicsGetCurrentContext(); | |
CGContextClipToRect(context, newFrame); | |
// 转换坐标系:quartz 2D的坐标系原点在屏幕左下角,方向向上 | |
// 转换成和UIKit坐标系一致需要 1.旋转180度; 2.向上平移height | |
// 3.水平翻转需要将坐标系向左移动width | |
CGContextRotateCTM(context, M_PI); // 反方向 | |
CGContextTranslateCTM(context, -newFrame.size.width, -newFrame.size.height); // 将 2、3步合并 | |
// 不做上述转换,重绘时默认是垂直翻转 | |
CGContextDrawImage(context, newFrame, self.CGImage); | |
UIImage *resultImage = UIGraphicsGetImageFromCurrentImageContext(); | |
UIGraphicsEndImageContext(); | |
UIImage *flipedImage = [UIImage imageWithCGImage:resultImage.CGImage scale:self.scale orientation:self.imageOrientation]; | |
return flipedImage; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment