Image gets blurry and zoomed out when erasing
Asked Answered
P

0

6

I am using functionality of erasing image and my code is like below. You can see Video HERE. Here current_sticker_img is my Imageview

NOTE: I am also using Pan Gesture for zooming image

 //MARK: Eraser touch event
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        if btn_eraser.isSelected == true || btn_repaint.isSelected == true{
            let touch : UITouch = touches.first!
            lastpoint = touch.location(in: current_sticker_img)
        }
    }
    override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
        if btn_eraser.isSelected == true || btn_repaint.isSelected == true{
            let touch = touches.first!
            let currentPoint : CGPoint = touch.location(in: current_sticker_img)
            let isPointInsideView = current_sticker_img.point(inside: currentPoint, with: event)
            if isPointInsideView
            {
                UIGraphicsBeginImageContext(current_sticker_img.frame.size)
//                UIGraphicsBeginImageContextWithOptions(CGSize(width: (current_sticker_img.image?.size.width)!, height: (current_sticker_img.image?.size.height)!), false, (current_sticker_img.image?.scale)!)

                current_sticker_img
                    .image?.draw(in: CGRect(x: 0, y: 0, width: (current_sticker_img.frame.size.width), height: (current_sticker_img.frame.size.height)))
                UIGraphicsGetCurrentContext()!.setLineCap(.round)
                UIGraphicsGetCurrentContext()?.setLineWidth(Current_slider_value_for_Eraser)

                UIGraphicsGetCurrentContext()?.setShadow(offset: CGSize(width: CGFloat(0), height: CGFloat(0)), blur: 0, color: UIColor.clear.cgColor )
                UIGraphicsGetCurrentContext()!.setBlendMode(.clear)

                UIGraphicsGetCurrentContext()?.move(to: CGPoint(x: CGFloat(lastpoint.x), y: CGFloat(lastpoint.y)))
                UIGraphicsGetCurrentContext()?.addLine(to: CGPoint(x: CGFloat(currentPoint.x), y: CGFloat(currentPoint.y)))
                UIGraphicsGetCurrentContext()!.strokePath()

                current_sticker_img.image = UIGraphicsGetImageFromCurrentImageContext()
                UIGraphicsEndImageContext()
                lastpoint = currentPoint
            }
        }
    }

What I am doing wrong ? Please help me with it. Thank you

Polyandrous answered 31/3, 2017 at 10:4 Comment(7)
And what your pan gesture related code does ?Jugate
@Jugate just simple for zooming in and zooming out imagePolyandrous
Ok but as your problem seems to be exactly related to the zoom out feature. Have you tried to use these delegate methods: func gesture​Recognizer(UIGesture​Recognizer, should​Recognize​Simultaneously​With:​ UIGesture​Recognizer) or func gesture​Recognizer(UIGesture​Recognizer, should​Receive:​ UITouch) ? in order to make sure the pan gesture doesn't perform any action. And why are you using Pan Gesture for zooming and not Pinch Gesture ?Jugate
I am using this library cocoacontrols.com/controls/zdstickerview for it. This library give me all handling regarding to image. and in that image I used eraserPolyandrous
Have you try to set preventsResizing to YES on you StickerView just to be sure it doesn't come from this library ? Also could you log the image size of current_sticker_img.image before and juste after the current_sticker_img.image = UIGraphicsGetImageFromCurrentImageContext() line ?Jugate
No , I did not try prevent resizing and did not log currentimage.image but Sure I will try @JugatePolyandrous
I used preventresizing but same problem occurs, I dont want like that sticker will resize if user want to zoom in or out.Polyandrous

© 2022 - 2024 — McMap. All rights reserved.