国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > iOS > 正文

iOS 圖片裁剪 + 旋轉

2020-07-26 02:54:15
字體:
來源:轉載
供稿:網友

之前分別介紹了圖片裁剪和圖片旋轉方法

《iOS 圖片裁剪方法》

地址:http://www.survivalescaperooms.com/article/107308.htm

《iOS 圖片旋轉方法》

地址:http://www.survivalescaperooms.com/article/107361.htm

裁剪和旋轉是可以連在一起執行的。先定位到需要裁剪的區域,然后以此區域的中心為軸,旋轉一定角度,最后獲取旋轉后此區域內的圖片??梢杂梦粓D(Bitmap)繪制實現

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為需要裁剪的區域,采用原圖的坐標系。angle為需要旋轉的角度,單位是度,正值表示圖片順時針方向旋轉。具體實現的講解見注釋。

最后的圖片可能超出原圖區域,超出部分為透明。

以上就是本文的全部內容,希望本文的內容對大家的學習或者工作能帶來一定的幫助,同時也希望多多支持武林網!

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 江油市| 安仁县| 来安县| 普安县| 信宜市| 白朗县| 巢湖市| 陈巴尔虎旗| 云南省| 舟山市| 道孚县| 云龙县| 仲巴县| 兴仁县| 大安市| 瓦房店市| 乐陵市| 怀宁县| 阳城县| 奇台县| 缙云县| 静宁县| 惠来县| 辛集市| 托克逊县| 柳林县| 华容县| 大冶市| 天全县| 乐至县| 潮州市| 叙永县| 洞口县| 青田县| 正安县| 铁岭县| 郎溪县| 琼海市| 旅游| 通山县| 临潭县|