iOS 圖片旋轉(zhuǎn)方法實(shí)例代碼
通過(guò) CGImage 或 CIImage 旋轉(zhuǎn)特定角度
UIImage可通過(guò)CGImage或CIImage初始化,初始化方法分別為init(cgImage: CGImage, scale: CGFloat, orientation: UIImageOrientation)和init(ciImage: CIImage, scale: CGFloat, orientation: UIImageOrientation)
。通過(guò)UIImageOrientation的不同取值,可以使圖片旋轉(zhuǎn)90、180、270度。
用原圖繪制
通過(guò)原圖繪制實(shí)現(xiàn)旋轉(zhuǎn)圖片任意角度??梢韵壤L制紅色背景,效果如下
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 }
如果旋轉(zhuǎn)的角度能被360整除,則不需要旋轉(zhuǎn),直接返回原圖。如果是其他角度,需要進(jìn)行繪制。
繪制首先要獲取原點(diǎn)為零、大小為原圖大小的CGRect,用imageRect表示。CGAffineTransform.identity獲得單位矩陣。CGAffineTransform的rotated(by angle: CGFloat) -> CGAffineTransform
方法將矩陣旋轉(zhuǎn)一定角度,返回旋轉(zhuǎn)后的矩陣。角度采用弧度制,正值為逆時(shí)針?lè)较?,?fù)值為順時(shí)針?lè)较?。CGRect的applying(_ t: CGAffineTransform) -> CGRect方法將旋轉(zhuǎn)后的矩陣用于imageRect,返回包含imageRect旋轉(zhuǎn)后的最小CGRect,用rotatedRect表示,作為位圖大小。rotatedRect的原點(diǎn)可能不為零,需要置為零。
位圖的CGContext以原點(diǎn)為軸旋轉(zhuǎn)。為了使圖片以中心為軸旋轉(zhuǎn),先把CGContext的原點(diǎn)移至中心context.translateBy(x: rotatedRect.width / 2, y: rotatedRect.height / 2)
,然后再旋轉(zhuǎn)context.rotate(by: radian)。CGContext的rotate(by angle: CGFloat)方法也是采用弧度制,正值表示context逆時(shí)針?lè)较蛐D(zhuǎn),繪制出來(lái)的效果為圖片順時(shí)針?lè)较蛐D(zhuǎn)。此時(shí),context的原點(diǎn)在位圖的中心,需要按照原圖大小的一半進(jìn)行位移,context.translateBy(x: -image.size.width / 2, y: -image.size.height / 2)
,使整張圖從原點(diǎn)繪制后圖的中心在位圖區(qū)域的中心。
如果要得到紅色背景,則在取得context后立即填充紅色,即在guard let context = UIGraphicsGetCurrentContext() else { return nil }
后加上
UIColor.red.setFill() context.fill(rotatedRect)
通過(guò) CALayer 繪制
可以將圖片放在UIView上,用CALayer繪制旋轉(zhuǎn)后的圖片。
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 }
將原圖放入U(xiǎn)IImageView,用imageView表示,然后進(jìn)行矩陣旋轉(zhuǎn)。獲取旋轉(zhuǎn)后的CGRect,創(chuàng)建一個(gè)相同大小的UIView,用containerView表示,作為imageView的父視圖(superview)。將imageView居中放置。用containerView的layer進(jìn)行繪制。
如果要得到紅色背景,則在創(chuàng)建containerView后設(shè)置背景色,即在let containerView = UIView(frame: CGRect(origin: .zero, size: rotatedRect.size))
后加上
containerView.backgroundColor = .red
以上所述是小編給大家介紹的iOS 圖片旋轉(zhuǎn)方法實(shí)例代碼,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
- iOS手勢(shì)識(shí)別的詳細(xì)使用方法(拖動(dòng),縮放,旋轉(zhuǎn),點(diǎn)擊,手勢(shì)依賴(lài),自定義手勢(shì))
- iOS拍照后圖片自動(dòng)旋轉(zhuǎn)90度的完美解決方法
- JS解決IOS中拍照?qǐng)D片預(yù)覽旋轉(zhuǎn)90度BUG的問(wèn)題
- iOS 圖片裁剪 + 旋轉(zhuǎn)
- iOS應(yīng)用開(kāi)發(fā)中使用UIScrollView控件來(lái)實(shí)現(xiàn)圖片縮放
- iOS利用UIScrollView實(shí)現(xiàn)圖片的縮放實(shí)例代碼
- iOS中大尺寸圖片的旋轉(zhuǎn)與縮放實(shí)例詳解
相關(guān)文章
IOS 改變導(dǎo)航欄返回按鈕的標(biāo)題實(shí)例詳解
這篇文章主要介紹了IOS 改變導(dǎo)航欄返回按鈕的標(biāo)題實(shí)例詳解的相關(guān)資料,需要的朋友可以參考下2017-04-04iOS開(kāi)發(fā)教程之XLForm的基本使用方法
XLForm 是最靈活且最強(qiáng)大的創(chuàng)建動(dòng)態(tài)表單的iOS庫(kù),下面這篇文章主要給大家介紹了關(guān)于iOS開(kāi)發(fā)教程之XLForm的基本使用方法,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧。2018-04-04關(guān)于iOS GangSDK的使用 為App快速集成社群公會(huì)模塊
這篇文章主要介紹了iOS GangSDK的使用為App快速集成社群公會(huì)模塊功能的實(shí)現(xiàn)過(guò)程。2017-11-11iOS中使用UItableviewcell實(shí)現(xiàn)團(tuán)購(gòu)和微博界面的示例
這篇文章主要介紹了iOS中使用UItableviewcell實(shí)現(xiàn)團(tuán)購(gòu)和微博界面的示例,開(kāi)發(fā)語(yǔ)言基于傳統(tǒng)的Objective-C,需要的朋友可以參考下2016-01-01iOS UIScrollView滾動(dòng)視圖/無(wú)限循環(huán)滾動(dòng)/自動(dòng)滾動(dòng)的實(shí)例代碼
這篇文章主要介紹了iOS UIScrollView滾動(dòng)視圖/無(wú)限循環(huán)滾動(dòng)/自動(dòng)滾動(dòng),需要的朋友可以參考下2017-02-02解決移動(dòng)端 ios 系統(tǒng)鍵盤(pán)遮擋的問(wèn)題
下面小編就為大家分享一篇解決移動(dòng)端 ios 系統(tǒng)鍵盤(pán)遮擋的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2017-12-12