【问题标题】:Drawing on UIImageView within UIScrollView在 UIScrollView 中的 UIImageView 上绘图
【发布时间】:2016-05-26 20:56:53
【问题描述】:

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

}

在图像视图上绘图:

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))!

}

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

【问题讨论】:

    标签: ios swift uiscrollview uiimageview drawing


    【解决方案1】:

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

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

    请注意,您绘制线的方式效率很低。相反,您可能想要创建一个 CGBitmapContext 并不断绘制它;它会快得多,并且还可以消除您遇到的“代损失”问题。

    【讨论】:

    • 我试过你提供的代码;但是,当我尝试使用钢笔时,objectView 变黑了……你知道为什么会发生这种情况吗?
    • 您可以尝试在初始化程序中使用false 而不是true(指定图像是否不透明):我使用true 是因为性能提升,但因为您没有填充背景颜色为白色,可能默认为黑色!
    • (我改了答案。)
    • 对不起,我不知道你在说什么初始化程序。我还注意到 objectView 不再填满屏幕。我该怎么办?
    • @CharltonProvatas 我想出了一个解决方法:在您绘图时,禁用滚动视图上的滚动。然后重新启用它。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多