Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 在UIScrollView中的UIImageView上绘图_Ios_Swift_Uiscrollview_Uiimageview_Drawing - Fatal编程技术网

Ios 在UIScrollView中的UIImageView上绘图

Ios 在UIScrollView中的UIImageView上绘图,ios,swift,uiscrollview,uiimageview,drawing,Ios,Swift,Uiscrollview,Uiimageview,Drawing,关于我的应用程序:用户可以在UIWebView中查看PDF文件。我有一个选项供用户选择是否要滚动浏览pdf或在其上做笔记。当他们记笔记时,滚动被禁用,反之亦然。但是,当用户绘制时,线条向上移动并变得模糊,如图所示: (红色框是pdf中的文本) 这是我的密码: 在笔和滚动之间切换: var usingPen = false @IBAction func usePen(sender: AnyObject) { usingPen = true webView.userInteract

关于我的应用程序:用户可以在
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
    }

}
用户绘制的图像视图(
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))!

}
var start=CGPoint()
let size:CGFloat=3
var color=UIColor.blackColor()
函数绘制(起点:CGPoint,终点:CGPoint){
如果使用pen==true{
UIGraphicsBeginImageContext(self.objectView.frame.size)
let context=UIGraphicsGetCurrentContext()
objectView.image?.drawInRect(CGRect(x:0,y:0,宽度:objectView.frame.width,高度:objectView.frame.height))
CGContextSetFillColorWithColor(上下文,color.CGColor)
CGContextSetStrokeColorWithColor(上下文,color.CGColor)
CGContextSetLineWidth(上下文、大小)
CGContextBeginPath(上下文)
CGContextMoveToPoint(上下文,start.x,start.y)
CGContextAddLineToPoint(上下文,end.x,end.y)
CGContextStrokePath(上下文)
让newImage=UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsSendImageContext()
objectView.image=newImage
}
}
覆盖功能触摸开始(触摸:设置,withEvent事件:UIEvent?){
开始=(touchs.first?.locationInView(self.objectView))!
}
覆盖功能触摸移动(触摸:设置,带事件:UIEvent?){
绘制(开始、结束:(touch.first?.locationInView(self.objectView))!)
开始=(touchs.first?.locationInView(self.objectView))!
}

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

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

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

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

我尝试了您提供的代码;然而,当我尝试使用钢笔时,objectView变为黑色。。。您知道为什么会出现这种情况吗?您可以尝试在初始化器中使用
false
而不是
true
(用于指定图像是否不透明):我使用
true
是因为性能提高,但由于您没有将背景颜色填充为白色,因此它可能默认为黑色!(我改变了答案)对不起,我不知道你说的是什么初始值设定项。我还注意到objectView不再占据屏幕。我该怎么办?@CharltonProvatas我想出了一个解决办法:在绘图时,禁用滚动视图上的滚动。然后重新启用它。
UIGraphicsBeginImageContextWithOptions(self.objectView.frame.size, false, UIScreen.mainScreen().scale)