iOS:由UIBezierPath创建的UIImage不顺畅

时间:2016-01-17 07:41:35

标签: ios swift

我从触摸屏获得了这个点,并缩放了点,然后我将该点指向UIBezierPath

这是我的代码

let bezierPath = UIBezierPath()

for var i:Int = 0 ; i < newPoints.count ; i++ {
   var indexCount = 0
   let point = newPoints[i][j]
   if point.count > 0{
      let newPointX = point[0] - originRectX // reset the point start with CGPointZero
      let newPointY = point[1] - originRectY
      let scalePointX = newPointX * heightScale  //scale the point with heightScale 0.25
      let scalePointY = newPointY * heightScale
      let scalePoint = CGPointMake(scalePointX, scalePointY)
      if indexCount == 0 {
         bezierPath.moveToPoint(scalePoint)
      }else{
         bezierPath.addLineToPoint(scalePoint)
      }
      indexCount++
   }
}

然后我将路径转换为UIImage

var tempImage = UIImage()
UIGraphicsBeginImageContextWithOptions(size, false, 1)
stokeColor.setStroke()
bezierPath.lineWidth = lineWidth
bezierPath.lineCapStyle = CGLineCap.Round
bezierPath.lineJoinStyle = CGLineJoin.Round
bezierPath.stroke()
tempImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()

然后我在屏幕上绘制UIImage

 let context = UIGraphicsGetCurrentContext();
 CGContextSaveGState(context)
 let rect = CGRect(x: drawObj.points![0].x,y: drawObj.points![0].y,width: drawObj.image.size.width,height: drawObj.image.size.height)
 drawObj.image?.drawInRect(rect)
 CGContextRotateCTM(context,CGFloat(Double(drawObj.angle!)*M_PI/180))
 CGContextRestoreGState(context)

这是效果

enter image description here

你会发现它不顺畅

1 个答案:

答案 0 :(得分:2)

阅读documentation了解您正在呼叫的功能。

UIGraphicsBeginImageContextWithOptions(size, false, 1)

第三个参数是scale。你传入1,这是一个像原始iPhone一样的1倍显示器的比例。在2016年,您几乎肯定会使用主屏幕为2x或3x的设备。最简单的解决方法是将参数更改为0,这将自动使用主屏幕的比例:

UIGraphicsBeginImageContextWithOptions(size, false, 0)

此外,虽然我们在这里,但这条线没有任何效果:

CGContextRotateCTM(context,CGFloat(Double(drawObj.angle!)*M_PI/180))

如果您希望将其应用于图片的绘图,则需要先调用,然后调用drawObj.image?.drawInRect(rect)