iOS 圖片裁剪 + 旋轉(zhuǎn)
之前分別介紹了圖片裁剪和圖片旋轉(zhuǎn)方法
《iOS 圖片裁剪方法》
地址:http://www.dhdzp.com/article/107308.htm
《iOS 圖片旋轉(zhuǎn)方法》
地址:http://www.dhdzp.com/article/107361.htm
裁剪和旋轉(zhuǎn)是可以連在一起執(zhí)行的。先定位到需要裁剪的區(qū)域,然后以此區(qū)域的中心為軸,旋轉(zhuǎn)一定角度,最后獲取旋轉(zhuǎn)后此區(qū)域內(nèi)的圖片。可以用位圖(Bitmap)繪制實(shí)現(xiàn)
static func cropImage(_ image: UIImage, withRect rect: CGRect, angle: Double) -> UIImage? {
// Creates a bitmap-based graphics context with rect size
// and makes it the current context
UIGraphicsBeginImageContext(rect.size)
// Get current graphics context
guard let context = UIGraphicsGetCurrentContext() else { return nil }
// Move context origin to rect center
context.translateBy(x: rect.width / 2, y: rect.height / 2)
// Convert angle to radian and rotate
context.rotate(by: CGFloat(angle / 180 * M_PI))
// Move context origin back (-rect.width / 2, -rect.height / 2)
// and move opposite direction of rect origin (-rect.minX, -rect.minY)
context.translateBy(x: -rect.width / 2 - rect.minX, y: -rect.height / 2 - rect.minY)
// Draw image at context origin
image.draw(at: .zero)
// Get image
let finalImage = UIGraphicsGetImageFromCurrentImageContext()
// Removes the current bitmap-based graphics context from the top of the stack
UIGraphicsEndImageContext()
// Return image
return finalImage
}
rect為需要裁剪的區(qū)域,采用原圖的坐標(biāo)系。angle為需要旋轉(zhuǎn)的角度,單位是度,正值表示圖片順時(shí)針方向旋轉(zhuǎn)。具體實(shí)現(xiàn)的講解見注釋。
最后的圖片可能超出原圖區(qū)域,超出部分為透明。
以上就是本文的全部內(nèi)容,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作能帶來一定的幫助,同時(shí)也希望多多支持腳本之家!
相關(guān)文章
詳解2016 cocoapods的安裝和使用以及版本升級(jí)遇到的問題
CocoaPods是一個(gè)負(fù)責(zé)管理iOS項(xiàng)目中第三方開源庫的工具。這篇文章主要介紹了2016 cocoapods的安裝和使用以及版本升級(jí)遇到的問題,有需要的可以了解一下。2016-12-12
IOS中NSPredicate和NSRegularExpression校驗(yàn)正則表達(dá)式區(qū)別
本文文章通過實(shí)例代碼給大家講述了在IOS開發(fā)中NSPredicate和NSRegularExpression校驗(yàn)正則表達(dá)式區(qū)別,需要的朋友趕快學(xué)習(xí)下吧。2018-01-01
iOS使用UICountingLabel實(shí)現(xiàn)數(shù)字變化的動(dòng)畫效果
本文主要介紹了iOS使用UICountingLabel實(shí)現(xiàn)數(shù)字變化動(dòng)畫效果的方法,具有一定的參考價(jià)值,下面跟著小編一起來看下吧2016-12-12
iOS開發(fā)中UITableview控件的基本使用及性能優(yōu)化方法
這篇文章主要介紹了iOS開發(fā)中UITableview控件的基本使用及性能優(yōu)化方法,代碼基于傳統(tǒng)的Objective-C,需要的朋友可以參考下2015-12-12
iOS開發(fā)教程之識(shí)別圖片中二維碼功能的實(shí)現(xiàn)
長按識(shí)別二維碼這個(gè)功能相信對(duì)大家來說都不陌生,最近工作中就遇到了這個(gè)需求,所以下面這篇文章主要給大家介紹了關(guān)于利用iOS識(shí)別圖片中二維碼的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考下2018-07-07

