iOS 圖片旋轉方法


更多圖片處理方法見圖片組件 BBWebImage

iOS 圖片旋轉方法

通過 CGImage 或 CIImage 旋轉特定角度

UIImage可通過CGImageCIImage初始化,初始化方法分別為init(cgImage: CGImage, scale: CGFloat, orientation: UIImageOrientation)init(ciImage: CIImage, scale: CGFloat, orientation: UIImageOrientation)。通過UIImageOrientation的不同取值,可以使圖片旋轉90、180、270度。

用原圖繪制

通過原圖繪制實現旋轉圖片任意角度。可以先繪制紅色背景,效果如下

static func rotateImage(_ image: UIImage, withAngle angle: Double) -> UIImage? {
	if angle.truncatingRemainder(dividingBy: 360) == 0 { return image }
	
	let imageRect = CGRect(origin: .zero, size: image.size)
	let radian = CGFloat(angle / 180 * M_PI)
	let rotatedTransform = CGAffineTransform.identity.rotated(by: radian)
	var rotatedRect = imageRect.applying(rotatedTransform)
	rotatedRect.origin.x = 0
	rotatedRect.origin.y = 0
	
	UIGraphicsBeginImageContext(rotatedRect.size)
	guard let context = UIGraphicsGetCurrentContext() else { return nil }
	context.translateBy(x: rotatedRect.width / 2, y: rotatedRect.height / 2)
	context.rotate(by: radian)
	context.translateBy(x: -image.size.width / 2, y: -image.size.height / 2)
	image.draw(at: .zero)
	let rotatedImage = UIGraphicsGetImageFromCurrentImageContext()
	UIGraphicsEndImageContext()
	return rotatedImage
}

angle表示需要旋轉的角度,單位是度,正值表示圖片順時針方向旋轉。如果旋轉的角度能被360整除,則不需要旋轉,直接返回原圖。如果是其他角度,需要進行繪制。

繪制首先要獲取原點為零、大小為原圖大小的CGRect,用imageRect表示。CGAffineTransform.identity獲得單位矩陣。CGAffineTransformrotated(by angle: CGFloat) -> CGAffineTransform方法將矩陣旋轉一定角度,返回旋轉后的矩陣。角度采用弧度制,正值為逆時針方向,負值為順時針方向。CGRectapplying(_ t: CGAffineTransform) -> CGRect方法將旋轉后的矩陣用於imageRect,返回包含imageRect旋轉后的最小CGRect,用rotatedRect表示,作為位圖大小。rotatedRect的原點可能不為零,需要置為零。

位圖的CGContext以原點為軸旋轉。為了使圖片以中心為軸旋轉,先把CGContext的原點移至中心context.translateBy(x: rotatedRect.width / 2, y: rotatedRect.height / 2),然后再旋轉context.rotate(by: radian)CGContextrotate(by angle: CGFloat)方法也是采用弧度制,正值表示context逆時針方向旋轉,繪制出來的效果為圖片順時針方向旋轉。此時,context的原點在位圖的中心,需要按照原圖大小的一半進行位移,context.translateBy(x: -image.size.width / 2, y: -image.size.height / 2),使整張圖從原點繪制后圖的中心在位圖區域的中心。

如果要得到紅色背景,則在取得context后立即填充紅色,即在guard let context = UIGraphicsGetCurrentContext() else { return nil }后加上

UIColor.red.setFill()
context.fill(rotatedRect)

通過 CALayer 繪制

可以將圖片放在UIView上,用CALayer繪制旋轉后的圖片。

static func rotateImage(_ image: UIImage, withAngle angle: Double) -> UIImage? {
	if angle.truncatingRemainder(dividingBy: 360) == 0 { return image }
	
	let imageView = UIImageView(image: image)
	imageView.transform = CGAffineTransform.identity.rotated(by: CGFloat(angle / 180 * M_PI))
	let rotatedRect = imageView.bounds.applying(imageView.transform)
	let containerView = UIView(frame: CGRect(origin: .zero, size: rotatedRect.size))
	imageView.center = containerView.center
	containerView.addSubview(imageView)
	
	UIGraphicsBeginImageContext(containerView.bounds.size)
	guard let context = UIGraphicsGetCurrentContext() else { return nil }
	containerView.layer.render(in: context)
	let rotatedImage = UIGraphicsGetImageFromCurrentImageContext()
	UIGraphicsEndImageContext()
	return rotatedImage
}

將原圖放入UIImageView,用imageView表示,然后進行矩陣旋轉。獲取旋轉后的CGRect,創建一個相同大小的UIView,用containerView表示,作為imageView的父視圖(superview)。將imageView居中放置。用containerViewlayer進行繪制。

如果要得到紅色背景,則在創建containerView后設置背景色,即在let containerView = UIView(frame: CGRect(origin: .zero, size: rotatedRect.size))后加上

containerView.backgroundColor = .red

轉載請注明出處:http://www.cnblogs.com/silence-cnblogs/p/6496564.html


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM