iOS 圖片裁剪 + 旋轉(zhuǎn)
之前分別介紹了圖片裁剪和圖片旋轉(zhuǎn)方法
《iOS 圖片裁剪方法》
地址:http://www.dbjr.com.cn/article/107308.htm
《iOS 圖片旋轉(zhuǎn)方法》
地址:http://www.dbjr.com.cn/article/107361.htm
裁剪和旋轉(zhuǎn)是可以連在一起執(zhí)行的。先定位到需要裁剪的區(qū)域,然后以此區(qū)域的中心為軸,旋轉(zhuǎn)一定角度,最后獲取旋轉(zhuǎn)后此區(qū)域內(nèi)的圖片??梢杂梦粓D(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í)針?lè)较蛐D(zhuǎn)。具體實(shí)現(xiàn)的講解見(jiàn)注釋。
最后的圖片可能超出原圖區(qū)域,超出部分為透明。
以上就是本文的全部?jī)?nèi)容,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作能帶來(lái)一定的幫助,同時(shí)也希望多多支持腳本之家!
相關(guān)文章
iOS中一行代碼實(shí)現(xiàn) UIView 鏤空效果
這篇文章主要介紹了一行代碼實(shí)現(xiàn) UIView 鏤空效果,這是一種實(shí)現(xiàn) UIView 鏤空效果的方案,可以快速實(shí)現(xiàn)任意形狀的鏤空、文字的鏤空、帶鏤空的毛玻璃效果等。需要的朋友可以參考下2018-11-11Objective-C 自定義漸變色Slider的實(shí)現(xiàn)方法
系統(tǒng)提供UISlider,但在開(kāi)發(fā)過(guò)程中經(jīng)常需要自定義,本次需求內(nèi)容是實(shí)現(xiàn)一個(gè)擁有漸變色的滑動(dòng)條,且漸變色隨著手指touch的位置不同改變區(qū)域,這篇文章主要介紹了Objective-C 自定義漸變色Slider,需要的朋友可以參考下2024-07-07移動(dòng)端固定輸入框在底部會(huì)被鍵盤(pán)遮擋的解決方法(必看篇)
下面小編就為大家分享關(guān)于移動(dòng)端固定輸入框在底部會(huì)被鍵盤(pán)遮擋的解決方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2017-12-12ajax 三種實(shí)現(xiàn)方法實(shí)例代碼
這篇文章主要介紹了ajax 三種實(shí)現(xiàn)方法實(shí)例代碼的相關(guān)資料,需要的朋友可以參考下2016-09-09