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

Jitendra Modi
  • 2,344
  • 12
  • 34
  • And what your pan gesture related code does ? – cire.boroguies Apr 06 '17 at 13:27
  • @cire.boroguies just simple for zooming in and zooming out image – Jitendra Modi Apr 07 '17 at 04:27
  • 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 ? – cire.boroguies Apr 07 '17 at 13:09
  • I am using this library https://www.cocoacontrols.com/controls/zdstickerview for it. This library give me all handling regarding to image. and in that image I used eraser – Jitendra Modi Apr 08 '17 at 04:12
  • 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 ? – cire.boroguies Apr 10 '17 at 13:45
  • No , I did not try prevent resizing and did not log currentimage.image but Sure I will try @cire.boroguies – Jitendra Modi Apr 11 '17 at 08:30
  • I used preventresizing but same problem occurs, I dont want like that sticker will resize if user want to zoom in or out. – Jitendra Modi Apr 11 '17 at 09:46

0 Answers0