gpt4 book ai didi

ios - 在 UIScrollView 中绘制 UIImageView

转载 作者:行者123 更新时间:2023-11-30 13:42:16 25 4
gpt4 key购买 nike

关于我的应用:用户可以在 UIWebView 中查看 PDF 文件。我有一个选项供用户选择是否要滚动浏览 pdf 或在其上做笔记。当他们做笔记时,滚动将被禁用,反之亦然。但是,当用户绘图时,线条会向上移动并变得模糊,如下所示: The pic(红框是pdf中的文本)

这是我的代码:

在笔和滚动之间切换:

var usingPen = false
@IBAction func usePen(sender: AnyObject) {

usingPen = true
webView.userInteractionEnabled = false
UIView.animateWithDuration(0.3) { () -> Void in
self.popUpView.alpha = 0
}

}

@IBAction func useScroll(sender: AnyObject) {

usingPen = false
webView.userInteractionEnabled = true
UIView.animateWithDuration(0.3) { () -> Void in
self.popUpView.alpha = 0
}

}

用户绘制的imageView(objectView):

var objectView = UIImageView()
override func viewDidAppear(animated: Bool) {

objectView.frame.size = webView.scrollView.contentSize
webView.scrollView.addSubview(objectView)

}

在 ImageView 上绘图:

var start = CGPoint()

let size: CGFloat = 3

var color = UIColor.blackColor()

func draw(start: CGPoint, end: CGPoint) {

if usingPen == true {

UIGraphicsBeginImageContext(self.objectView.frame.size)
let context = UIGraphicsGetCurrentContext()
objectView.image?.drawInRect(CGRect(x: 0, y: 0, width: objectView.frame.width, height: objectView.frame.height))
CGContextSetFillColorWithColor(context, color.CGColor)
CGContextSetStrokeColorWithColor(context, color.CGColor)
CGContextSetLineWidth(context, size)
CGContextBeginPath(context)
CGContextMoveToPoint(context, start.x, start.y)
CGContextAddLineToPoint(context, end.x, end.y)
CGContextStrokePath(context)

let newImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
objectView.image = newImage

}


}

override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {

start = (touches.first?.locationInView(self.objectView))!

}

override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {

draw(start, end: (touches.first?.locationInView(self.objectView))!)
start = (touches.first?.locationInView(self.objectView))!

}

如何防止图纸模糊和移动?感谢您的帮助。

最佳答案

这可能是由于图像缩放问题造成的。由于您正在绘制比例因子为 1(默认值)的图像,并且屏幕的比例因子为 2 或 3,因此每次复制和绘制线条时,线条都会继续稍微模糊。解决方案是在创建图像上下文时指定屏幕的比例:

UIGraphicsBeginImageContextWithOptions(self.objectView.frame.size, false, UIScreen.mainScreen().scale)

请注意,您绘制界限的方式效率相当低。相反,您可能想要创建一个 CGBitmapContext 并不断地绘制它;它会更快,并且还会消除您遇到的“发电损失”问题。

关于ios - 在 UIScrollView 中绘制 UIImageView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35418001/

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