iOS圖片旋轉(zhuǎn)方法實例代碼

通過 CGImage 或 CIImage 旋轉(zhuǎn)特定角度

赤壁網(wǎng)站建設(shè)公司創(chuàng)新互聯(lián)公司,赤壁網(wǎng)站設(shè)計制作,有大型網(wǎng)站制作公司豐富經(jīng)驗。已為赤壁上千家提供企業(yè)網(wǎng)站建設(shè)服務(wù)。企業(yè)網(wǎng)站搭建\成都外貿(mào)網(wǎng)站建設(shè)要多少錢,請找那個售后服務(wù)好的赤壁做網(wǎng)站的公司定做!

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

用原圖繪制

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

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),直接返回原圖。如果是其他角度,需要進行繪制。

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

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

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

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

通過 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
}

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

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

containerView.backgroundColor = .red

以上所述是小編給大家介紹的iOS 圖片旋轉(zhuǎn)方法實例代碼,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對創(chuàng)新互聯(lián)網(wǎng)站的支持!

新聞標題:iOS圖片旋轉(zhuǎn)方法實例代碼
URL網(wǎng)址:http://muchs.cn/article34/pihcse.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供定制開發(fā)、網(wǎng)站建設(shè)、網(wǎng)站內(nèi)鏈用戶體驗、營銷型網(wǎng)站建設(shè)、網(wǎng)站導(dǎo)航

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

網(wǎng)站建設(shè)網(wǎng)站維護公司