gpt4 book ai didi

ios - ImageView 上的平滑线条

转载 作者:行者123 更新时间:2023-11-28 07:21:36 25 4
gpt4 key购买 nike

我目前在 Xcode 中有一个项目允许用户在 ImageView 上绘图,它运行良好,我唯一的问题是绘制的线条不清晰并且非常模糊和僵硬。我试图实现一些我在周围看到的想法,但它们都在我无法工作的更旧版本的 swift 中。

Screenshot of the blury lines

import Foundation
import UIKit

class SecondViewController: UIViewController {


@IBOutlet weak var toolicon: UIButton!
@IBOutlet var imageView: UIImageView!


var lastPoint = CGPoint.zero
var swiped = false
var red:CGFloat = 0.00
var green:CGFloat = 0.00
var blue:CGFloat = 0.00
var alpha:CGFloat = 1
var lw:CGFloat = 2.5
var tool:UIImageView!
var isDrawing = true
override func viewDidLoad() {
super.viewDidLoad()

tool = UIImageView()
tool.frame = CGRect(x: self.view.bounds.size.width, y: self.view.bounds.height, width: 75, height: 75)
tool.image = #imageLiteral(resourceName: "blank")
self.view.addSubview(tool)
}

@IBAction func eraser(_ sender: AnyObject) {
if (isDrawing) {
(red,green,blue) = (1,1,1)
tool.image = #imageLiteral(resourceName: "eraser2")
toolicon.setImage(#imageLiteral(resourceName: "pen"), for: .normal)
(lw) = (50)


} else {
(red,green,blue) = (0,0,0)
tool.image = #imageLiteral(resourceName: "blank")
toolicon.setImage(#imageLiteral(resourceName: "eraser"), for: .normal)
(lw) = (2.5)
}

isDrawing = !isDrawing
}

func delay(_ delay:Double, closure:@escaping ()->()) {
let when = DispatchTime.now() + delay
DispatchQueue.main.asyncAfter(deadline: when, execute: closure)
}

@IBAction func save(_ sender: AnyObject) {
if let image = imageView.image {
UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil)
self.imageView.image = nil

// the alert view
let alert = UIAlertController(title: "", message: "Thank you, your guestbook entry has been saved. Redirecting you the home page", preferredStyle: .alert)
self.present(alert, animated: true, completion: nil)

// change to desired number of seconds (in this case 5 seconds)
let when = DispatchTime.now() + 2.5
DispatchQueue.main.asyncAfter(deadline: when){
// your code with delay
alert.dismiss(animated: true, completion: nil)

}
delay(3.0) {
// do stuff
self.performSegue(withIdentifier: "savetocam", sender: nil)
}
}
}
@IBAction func lineSize(_ sender: AnyObject) {
if sender.tag == 8 {
(lw) = (1)
} else if sender.tag == 9 {
(lw) = (2.5)
} else if sender.tag == 10 {
(lw) = (5)
}
}


override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
swiped = false
if let touch = touches.first {
lastPoint = touch.preciseLocation(in: self.view)
}
}

@IBAction func colorsPicked(_ sender: AnyObject) {
if sender.tag == 0 {
(red,green,blue) = (0,0,0)
} else if sender.tag == 1 {
(red,green,blue) = (0.25,0.00,0.39)
} else if sender.tag == 2 {
(red,green,blue) = (0.65,0.62,0.75)
} else if sender.tag == 3 {
(red,green,blue) = (1.00,0.00,0.00)
} else if sender.tag == 4 {
(red,green,blue) = (0.49,0.77,0.46)
} else if sender.tag == 5 {
(red,green,blue) = (0.0,0.68,0.94)
} else if sender.tag == 6 {
(red,green,blue) = (0.96,0.56,0.34)
} else if sender.tag == 7 {
(red,green,blue) = (1.00,0.95,0.00)
}
}



@IBAction func reset(_ sender: AnyObject) {
self.imageView.image = nil
}


func drawLines(fromPoint:CGPoint,toPoint:CGPoint) {

UIGraphicsBeginImageContext(self.view.frame.size)
imageView.image?.draw(in: CGRect(x: 0, y: 0, width: self.view.frame.width, height: self.view.frame.height))
let context = UIGraphicsGetCurrentContext()

context?.move(to:CGPoint(x: fromPoint.x, y: fromPoint.y))
context?.addLine(to: CGPoint(x: toPoint.x, y: toPoint.y))

if(isDrawing){
context?.setBlendMode(CGBlendMode.normal)
}
else{
context?.setBlendMode(CGBlendMode.clear)
}

tool.center = toPoint
context?.setLineCap(CGLineCap.round)
context?.setLineWidth(lw)
context?.setStrokeColor(UIColor(red: red, green: green, blue: blue, alpha: alpha).cgColor)

context?.strokePath()

imageView.image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
}

override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
swiped = true
if let touch = touches.first {
let currentPoint = touch.preciseLocation(in: self.view)
drawLines(fromPoint: lastPoint, toPoint: currentPoint)

lastPoint = currentPoint
}
}

override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
if !swiped {
drawLines(fromPoint: lastPoint, toPoint: lastPoint)
}
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}


}

最佳答案

您应该按如下方式设置您的 Core Graphics 上下文:

func drawLines(fromPoint:CGPoint,toPoint:CGPoint) {

//UIGraphicsBeginImageContext(self.view.frame.size)
UIGraphicsBeginImageContextWithOptions(self.view.frame.size, false, 0.0) //SET OPAQUENESS TO FALSE
imageView.image?.draw(in: CGRect(x: 0, y: 0, width: self.view.frame.width, height: self.view.frame.height))
let context = UIGraphicsGetCurrentContext()

context?.move(to:CGPoint(x: fromPoint.x, y: fromPoint.y))
context?.addLine(to: CGPoint(x: toPoint.x, y: toPoint.y))

if(isDrawing){
context?.setBlendMode(CGBlendMode.normal)
}
else{
context?.setBlendMode(CGBlendMode.clear)
}

tool.center = toPoint
context?.setShouldAntialias(true) //IF YOU TURN THIS TO FALSE YOU GET RID OF BLURRING BUT LINES MAY BE A BIT JIGGY
context?.interpolationQuality = .high //THIS MAKES LINES SMOOTHER
context?.setLineCap(CGLineCap.round)
context?.setFlatness(0.5)
context?.setMiterLimit(0.5) //Play around with this value it should smoothen the angled parts of the signatures.
context?.setLineWidth(lw)
context?.setStrokeColor(UIColor(red: red, green: green, blue: blue, alpha: alpha).cgColor)
context?.strokePath()

imageView.image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
}

另请查看其他可能也有帮助的设置: Apple Docs

关于ios - ImageView 上的平滑线条,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57853739/

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