网创优客建站品牌官网
为成都网站建设公司企业提供高品质网站建设
热线:028-86922220
成都专业网站建设公司

定制建站费用3500元

符合中小企业对网站设计、功能常规化式的企业展示型网站建设

成都品牌网站建设

品牌网站建设费用6000元

本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...

成都商城网站建设

商城网站建设费用8000元

商城网站建设因基本功能的需求不同费用上面也有很大的差别...

成都微信网站建设

手机微信网站建站3000元

手机微信网站开发、微信官网、微信商城网站...

建站知识

当前位置:首页 > 建站知识

iOS图片旋转方法实例代码

通过 CGImage 或 CIImage 旋转特定角度

赤壁网站建设公司创新互联公司,赤壁网站设计制作,有大型网站制作公司丰富经验。已为赤壁上千家提供企业网站建设服务。企业网站搭建\成都外贸网站建设要多少钱,请找那个售后服务好的赤壁做网站的公司定做!

UIImage可通过CGImage或CIImage初始化,初始化方法分别为init(cgImage: CGImage, scale: CGFloat, orientation: UIImageOrientation)和init(ciImage: CIImage, scale: CGFloat, orientation: UIImageOrientation)。通过UIImageOrientation的不同取值,可以使图片旋转90、180、270度。

用原图绘制

通过原图绘制实现旋转图片任意角度。可以先绘制红色背景,效果如下

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
}

如果旋转的角度能被360整除,则不需要旋转,直接返回原图。如果是其他角度,需要进行绘制。

绘制首先要获取原点为零、大小为原图大小的CGRect,用imageRect表示。CGAffineTransform.identity获得单位矩阵。CGAffineTransform的rotated(by angle: CGFloat) -> CGAffineTransform方法将矩阵旋转一定角度,返回旋转后的矩阵。角度采用弧度制,正值为逆时针方向,负值为顺时针方向。CGRect的applying(_ t: CGAffineTransform) -> CGRect方法将旋转后的矩阵用于imageRect,返回包含imageRect旋转后的最小CGRect,用rotatedRect表示,作为位图大小。rotatedRect的原点可能不为零,需要置为零。

位图的CGContext以原点为轴旋转。为了使图片以中心为轴旋转,先把CGContext的原点移至中心context.translateBy(x: rotatedRect.width / 2, y: rotatedRect.height / 2),然后再旋转context.rotate(by: radian)。CGContext的rotate(by angle: CGFloat)方法也是采用弧度制,正值表示context逆时针方向旋转,绘制出来的效果为图片顺时针方向旋转。此时,context的原点在位图的中心,需要按照原图大小的一半进行位移,context.translateBy(x: -image.size.width / 2, y: -image.size.height / 2),使整张图从原点绘制后图的中心在位图区域的中心。

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

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

通过 CALayer 绘制

可以将图片放在UIView上,用CALayer绘制旋转后的图片。

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表示,然后进行矩阵旋转。获取旋转后的CGRect,创建一个相同大小的UIView,用containerView表示,作为imageView的父视图(superview)。将imageView居中放置。用containerView的layer进行绘制。

如果要得到红色背景,则在创建containerView后设置背景色,即在let containerView = UIView(frame: CGRect(origin: .zero, size: rotatedRect.size))后加上

containerView.backgroundColor = .red

以上所述是小编给大家介绍的iOS 图片旋转方法实例代码,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对创新互联网站的支持!


文章题目:iOS图片旋转方法实例代码
文章URL:http://bjjierui.cn/article/pihcse.html

其他资讯