gpt4 book ai didi

iOS : UIImage created by UIBezierPath not smooth

转载 作者:搜寻专家 更新时间:2023-11-01 07:27:47 24 4
gpt4 key购买 nike

我从触摸屏获取点,缩放点,然后我把点放到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

你会发现它不平滑

最佳答案

阅读documentation对于您调用的函数。

UIGraphicsBeginImageContextWithOptions(size, false, 1)

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

UIGraphicsBeginImageContextWithOptions(size, false, 0)

此外,当我们在这里时,这一行实际上什么也没做:

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

如果你想让它应用于图像的绘制,你需要先调用它,你调用drawObj.image?.drawInRect(rect)之前。

关于iOS : UIImage created by UIBezierPath not smooth,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34836209/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com