use*_*509 2 smooth draw uiimageview swift
下面的代码在 UIImageView 上绘制点之间的直线。绘图渲染速度快,看起来不错,但可能会更好,所以我想修改代码以创建平滑的曲线。在过去的六周里,我在 Swift 中研究了许多示例,但成功率为零。
我必须修改以下代码的哪些部分(以及如何)来实现这一点?有人告诉我调用下面的代码片段而不是 CGContextAddLineToPoint() (在下面的 // 2 处)应该可以解决问题,但我不清楚如何调用它以及它如何与当前代码完全相关。
片段:
func drawQuadLineWithPoints(firstPoint: CGPoint, secondPoint: CGPoint, thirdPoint: CGPoint, inContext: CGContext) {
CGContextMoveToPoint(context, 0.5 * (firstPoint.x + secondPoint.x), 0.5 * (firstPoint.y + secondPoint.y));
CGContextAddQuadCurveToPoint(context, secondPoint.x, secondPoint.y, 0.5 * (secondPoint.x + thirdPoint.x), 0.5 * (secondPoint.y + thirdPoint.y));
}
Run Code Online (Sandbox Code Playgroud)
代码:
override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
swiped = false
if let touch = touches.anyObject() as? UITouch {
lastPoint = touch.locationInView(self.view)
}
}
func drawLineFrom(fromPoint: CGPoint, toPoint: CGPoint) {
// 1
UIGraphicsBeginImageContext(view.frame.size)
let context = UIGraphicsGetCurrentContext()
tempImageView.image?.drawInRect(CGRect(x: 0, y: 0, width: view.frame.size.width, height: view.frame.size.height))
// 2
CGContextMoveToPoint(context, fromPoint.x, fromPoint.y)
CGContextAddLineToPoint(context, toPoint.x, toPoint.y)
// 3
CGContextSetLineCap(context, kCGLineCapRound)
CGContextSetLineWidth(context, brushWidth)
CGContextSetRGBStrokeColor(context, red, green, blue, 1.0)
CGContextSetBlendMode(context, kCGBlendModeNormal)
// 4
CGContextStrokePath(context)
// 5
tempImageView.image = UIGraphicsGetImageFromCurrentImageContext()
tempImageView.alpha = opacity
UIGraphicsEndImageContext()
}
override func touchesMoved(touches: NSSet, withEvent event: UIEvent) {
// 6
swiped = true
if let touch = touches.anyObject() as? UITouch {
let currentPoint = touch.locationInView(view)
drawLineFrom(lastPoint, toPoint: currentPoint)
// 7
lastPoint = currentPoint
}
}
override func touchesEnded(touches: NSSet, withEvent event: UIEvent) {
if !swiped {
// draw a single point
drawLineFrom(lastPoint, toPoint: lastPoint)
}
// Merge tempImageView into mainImageView
UIGraphicsBeginImageContext(mainImageView.frame.size)
mainImageView.image?.drawInRect(CGRect(x: 0, y: 0, width: view.frame.size.width, height: view.frame.size.height), blendMode: kCGBlendModeNormal, alpha: 1.0)
tempImageView.image?.drawInRect(CGRect(x: 0, y: 0, width: view.frame.size.width, height: view.frame.size.height), blendMode: kCGBlendModeNormal, alpha: opacity)
mainImageView.image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
tempImageView.image = nil
}
Run Code Online (Sandbox Code Playgroud)
看看答案THIS
这是我使用的 Swift 的粗略翻译:
override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
if let touch = touches.first as? UITouch{
prevPoint1 = touch.previousLocationInView(self.view)
prevPoint2 = touch.previousLocationInView(self.view)
lastPoint = touch.locationInView(self.view)
}
}
override func touchesMoved(touches: Set<NSObject>, withEvent event: UIEvent) {
if let touch = touches.first as? UITouch{
let currentPoint = touch.locationInView(view)
prevPoint2 = prevPoint1
prevPoint1 = touch.previousLocationInView(self.view)
UIGraphicsBeginImageContext(view.frame.size)
let context = UIGraphicsGetCurrentContext()
TempImage.image?.drawInRect(CGRect(x: 0, y: 0, width: view.frame.size.width, height: view.frame.size.height))
var mid1 = CGPointMake((prevPoint1.x + prevPoint2.x)*0.5, (prevPoint1.y + prevPoint2.y)*0.5)
var mid2 = CGPointMake((currentPoint.x + prevPoint1.x)*0.5, (currentPoint.y + prevPoint1.y)*0.5)
CGContextMoveToPoint(context, mid1.x, mid1.y)
CGContextAddQuadCurveToPoint(context, prevPoint1.x, prevPoint1.y, mid2.x, mid2.y)
//CGContextAddLineToPoint(context, toPoint.x, toPoint.y)
CGContextSetLineCap(context, kCGLineCapRound)
CGContextSetLineWidth(context, brushWidth)
CGContextSetRGBStrokeColor(context, red, green,blue, 1.0)
CGContextSetBlendMode(context, kCGBlendModeNormal)
CGContextStrokePath(context)
TempImage.image = UIGraphicsGetImageFromCurrentImageContext()
TempImage.alpha = opacity
UIGraphicsEndImageContext()
lastPoint = currentPoint
}
}
override func touchesEnded(touches: Set<NSObject>, withEvent event: UIEvent) {
UIGraphicsBeginImageContext(MainImage.frame.size)
MainImage.image?.drawInRect(CGRect(x: 0, y: 0, width: view.frame.size.width, height: view.frame.size.height), blendMode: kCGBlendModeNormal, alpha: 1.0)
TempImage.image?.drawInRect(CGRect(x: 0, y: 0, width: view.frame.size.width, height: view.frame.size.height), blendMode: kCGBlendModeNormal, alpha: opacity)
MainImage.image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
TempImage.image = nil
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
4226 次 |
最近记录: |