大佬教程收集整理的这篇文章主要介绍了旋转UIImage的问题 – IOS / Swift,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
这是扩展代码
import UIKit extension UIImage { public func imageRotatedBydegrees(degrees: CGFloat,flip: Bool) -> UIImage { let radiansTodegrees: (CGFloat) -> CGFloat = { return $0 * (180.0 / CGFloat(M_PI)); } let degreesToradians: (CGFloat) -> CGFloat = { return $0 / 180.0 * CGFloat(M_PI); } // calculate the size of the rotated view's containing Box for our drawing space let rotatedViewBox = UIView(frame: CGRect(origin: CGPointZero,size: size)); let t = CGAffineTransformMakeRotation(degreesToradians(degrees)); rotatedViewBox.transform = t; let rotatedSize = rotatedViewBox.frame.size // Create the bitmap context UIGraphicsBeginImageContext(rotatedSize); let bitmap = UIGraphicsGetCurrentContext(); // Move the origin to the middle of the image so we will rotate and scale around the center. CGContextTranslateCTM(bitmap,rotatedSize.width / 2.0,rotatedSize.height / 2.0); // Rotate the image context CGContextRotateCTM(bitmap,degreesToradians(degrees)); // Now,draw the rotated/scaled image into the context var yFlip: CGFloat; if(flip){ yFlip = CGFloat(-1.0); } else { yFlip = CGFloat(1.0); } CGContextScaleCTM(bitmap,yFlip,-1.0); CGContextDrawImage(bitmap,CGRectMake(-size.width / 2,-size.height / 2,size.width,size.height),CGImage); let newImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return newImage; } }
这就是我调用扩展方法并将返回的图像设置为图像视图的方法.图像视图的’contentMode’属性设置为’Scale to Fill’
currentPagePreviewImageVew.image = currentPagePreviewImageVew.image!.imageRotatedBydegrees(90,flip: false);
这是按下旋转按钮之前的样子
这是在按下旋转按钮一次之后(参见图像未旋转但尺寸已更改)
这是当图像旋转90度时(图像旋转后,如图所示,当图像处于0和180度时,其他视图会被拉伸,如上面的屏幕(2))
有人可以告诉我我的代码有什么问题,如果有更好的解决方案,请告诉我.任何帮助将受到高度赞赏.
编辑:拉伸视图是因为存在约束问题.我通过在imageview上方的工具栏上设置一个高度约束来解决它.但仍然无法找到第一次的答案.
func flipImageVertical(originalImage: UIImage) -> UIImage { let tempImageView:UIImageView = UIImageView(image: originalImage) UIGraphicsBeginImageContext(tempImageView.frame.size) let context:CGContextRef = UIGraphicsGetCurrentContext()! let flipVertical:CGAffineTransform = CGAffineTransformMake(1,-1,tempImageView.frame.size.height) CGContextConcatCTM(context,flipVertical) tempImageView.layer .renderInContext(context) let flippedImage:UIImage = UIGraphicsGetImageFromCurrentImageContext() UIGraphicsEndImageContext() return flippedImage } func flipImageHorizontal(originalImage: UIImage) -> UIImage { let tempImageView:UIImageView = UIImageView(image: originalImage) UIGraphicsBeginImageContext(tempImageView.frame.size) let context:CGContextRef = UIGraphicsGetCurrentContext()! let flipHorizontal:CGAffineTransform = CGAffineTransformMake(-1,1,tempImageView.frame.size.width,0) CGContextConcatCTM(context,flipHorizontal) tempImageView.layer .renderInContext(context) let flippedImage:UIImage = UIGraphicsGetImageFromCurrentImageContext() UIGraphicsEndImageContext() return flippedImage }
以上是大佬教程为你收集整理的旋转UIImage的问题 – IOS / Swift全部内容,希望文章能够帮你解决旋转UIImage的问题 – IOS / Swift所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。