2016-08-30 10 views
0

イメージを消去しているときに正しく動作しています。しかし、画像をズームイン/アウトした後、消去しようとすると、画像は消去されますが同時にズームアウトされます。私は何をしていますか? ここに私のコードです。ズームした画像のiOSを消去しています。

import UIKit 
import CoreGraphics 
class ViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate { 
    var imageView = UIImageView() 
    var offsetImageView = UIImageView() 
    let offset = "Circle_Red.png" 
    let offsetImage = UIImage(named: "Circle_Red.png") 

    var eraserImageView = UIImageView() 
    var chosenImage = UIImage() 
    var swiped = false 
    var location = CGPoint(x: 0, y: 0) 
    var eraserEnabled = false 
    var lastPoint = CGPoint(x: 0, y: 0) 
    var isZoomable = false 

    var isMovable1 = true 
    var red: UIColor = UIColor.redColor() 
    var green: CGFloat = 0.0 
    var blue: CGFloat = 0.0 
    var opacity: CGFloat = 1.0 

    @IBOutlet weak var tempImageView: UIImageView! 

    @IBOutlet weak var myImageView: UIImageView! 
    let picker = UIImagePickerController() 

    @IBAction func chooseFromGallery(sender: UIBarButtonItem) { 
     picker.allowsEditing = false //2 
     picker.sourceType = .PhotoLibrary //3 
     picker.modalPresentationStyle = .Popover 
     presentViewController(picker, 
           animated: true, completion: nil)//4 
     picker.popoverPresentationController?.barButtonItem = sender 

    } 

    @IBAction func captureCamera(sender: UIBarButtonItem) { 
     if UIImagePickerController.availableCaptureModesForCameraDevice(.Rear) != nil { 
      picker.allowsEditing = false 
      picker.sourceType = UIImagePickerControllerSourceType.Camera 
      picker.cameraCaptureMode = .Photo 
      presentViewController(picker, animated: true, completion: nil) 
     } else { 
      noCamera() 
     } 
    } 

    func noCamera(){ 
     let alertVC = UIAlertController(
      title: "No Camera", 
      message: "Sorry, this device has no camera", 
      preferredStyle: .Alert) 
     let okAction = UIAlertAction(
      title: "OK", 
      style:.Default, 
      handler: nil) 
     alertVC.addAction(okAction) 
     presentViewController(alertVC, 
           animated: true, 
           completion: nil) 
    } 


    override func viewDidLoad() { 
     super.viewDidLoad() 
     picker.delegate = self 


     offsetImageView = UIImageView(image: offsetImage!) 
     offsetImageView.frame = CGRect(x: 0, y: 0, width: 15, height: 15) 
     view.addSubview(offsetImageView) 

     eraserImageView.frame = CGRect(x: 0, y: 0, width: CGFloat(eraserSize!), height: CGFloat(eraserSize!)) 
     view.addSubview(eraserImageView) 
     self.eraserImageView.layer.cornerRadius = 15.0 

     self.eraserImageView.clipsToBounds = true 

     self.eraserImageView.layer.borderWidth = 2.0 
     self.eraserImageView.layer.borderColor = UIColor.redColor().CGColor 
    } 

    override func didReceiveMemoryWarning() { 
     super.didReceiveMemoryWarning() 
    } 

    var imageHeight : CGFloat = 0.0 
    var imageWidth : CGFloat = 0.0 
    func imagePickerController(
     picker: UIImagePickerController, 
     didFinishPickingMediaWithInfo info: [String : AnyObject]) 
    { 
     let chosenImage = info[UIImagePickerControllerOriginalImage] as! UIImage //2 
     myImageView.contentMode = .ScaleAspectFit //3 
     myImageView.image = chosenImage //4 
     dismissViewControllerAnimated(true, completion: nil) //5  
     imageHeight = chosenImage.size.height 
     imageWidth = chosenImage.size.width 
    } 
    func imagePickerControllerDidCancel(picker: UIImagePickerController) { 
     dismissViewControllerAnimated(true, completion: nil) 
    } 

    @IBAction func share(sender: AnyObject) { 
     UIGraphicsBeginImageContext(myImageView.bounds.size) 
     myImageView.image?.drawInRect(CGRect(x: 0, y: 0, 
      width: myImageView.frame.size.width, height: myImageView.frame.size.height)) 
     let image = UIGraphicsGetImageFromCurrentImageContext() 
     UIGraphicsEndImageContext() 
     let activity = UIActivityViewController(activityItems: [image], applicationActivities: nil) 
     presentViewController(activity, animated: true, completion: nil) 
    } 


    @IBAction func zoomBtn(sender: UIBarButtonItem) { 
     isMovable1 = true 
     eraserEnabled = false 
     isZoomable = true 

    } 
    @IBAction func backgroundEraserBtn(sender: UIBarButtonItem) { 
     isMovable1 = false 
     isZoomable = false 
     eraserEnabled = true 
     print (imageHeight) 
     print (imageWidth) 

    } 


    var m = CGFloat() 

    @IBAction func zoomImage(sender: UIPinchGestureRecognizer) { 
     if isZoomable == true { 
      let m = CGAffineTransformScale(self.myImageView.transform, sender.scale, sender.scale) 

      self.myImageView.transform = m 
      sender.scale = 1 
     } 
    } 

    var eraserSize : Float? = 60//35.0 
    var offsetDistance : Float? = 120 //80.0 

    @IBAction func sliderSizeChanged(sender: UISlider) { 
     eraserSize = sender.value 
     print(eraserSize) 

     eraserImageView.frame = CGRect(x: 0, y: 0, width: CGFloat(eraserSize!), height: CGFloat(eraserSize!)) 
    } 

    @IBAction func sliderOffsetChanged(sender: AnyObject) { 
     offsetDistance = sender.value 
     print(offsetDistance) 
    } 

    override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {  
     if isMovable1 == true { 
      let touch: UITouch = touches.first! 
      location = touch.locationInView(self.view) 
      myImageView.center = location 
     } 

     if eraserEnabled == true 
     {    
      if let touch: UITouch = touches.first! { 
       lastPoint = touch.locationInView(self.view) 
       offsetImageView.center = lastPoint 
       location = touch.locationInView(self.view) 
       location.y = location.y - CGFloat(offsetDistance!) 
       eraserImageView.center = location 
      } 
     } 
    } 

    override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) { 
     if isMovable1 == true { 
     let touch: UITouch = touches.first! 
     location = touch.locationInView(self.view) 
     myImageView.center = location 
     }  
     if eraserEnabled == true 
     { 
      if let touch: UITouch = touches.first! 
      { 
       lastPoint = touch.locationInView(self.view) 
       offsetImageView.center = lastPoint 

       location = touch.locationInView(self.view) 
       location.y = location.y - CGFloat(offsetDistance!) 
       eraserImageView.center = location 

       let currentPoint = touch.locationInView(self.myImageView) 

       print(String(currentPoint) + " ") 

       UIGraphicsBeginImageContext(self.myImageView.frame.size) 

       self.myImageView.image?.drawInRect(CGRectMake(0, 0, self.myImageView.frame.width, self.myImageView.frame.height)) 

       CGContextSaveGState(UIGraphicsGetCurrentContext()) 
       CGContextSetShouldAntialias(UIGraphicsGetCurrentContext(), true) 
       CGContextSetLineCap(UIGraphicsGetCurrentContext(), CGLineCap.Round) 
       CGContextSetLineWidth(UIGraphicsGetCurrentContext(), CGFloat(eraserSize!)) 
       let path = CGPathCreateMutable() 

       CGPathMoveToPoint(path, nil, currentPoint.x, (currentPoint.y - CGFloat(offsetDistance!))) 

       print("lastpoint: " + String(lastPoint)) 
       CGPathAddLineToPoint(path, nil, currentPoint.x, (currentPoint.y - CGFloat(offsetDistance!))) 

       CGContextSetBlendMode(UIGraphicsGetCurrentContext(), CGBlendMode.Clear) 
       CGContextAddPath(UIGraphicsGetCurrentContext(), path) 
       CGContextStrokePath(UIGraphicsGetCurrentContext()) 

       myImageView.image = UIGraphicsGetImageFromCurrentImageContext() 
       CGContextRestoreGState(UIGraphicsGetCurrentContext()) 
       UIGraphicsEndImageContext() 

       lastPoint = currentPoint 
      } 
     } 
    } 

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

} 

@IBAction func continueBtn(sender: AnyObject) { 
    performSegueWithIdentifier("toAddAnotherPhotoAsBackground", sender: nil)   
} 
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { 
     if segue.identifier == "toAddAnotherPhotoAsBackground" { 


      let segueCtrl = segue.destinationViewController as! AddPhotoBackgroundViewController 

      segueCtrl.editedImage = myImageView.image 

     } 
    } 

}

+0

ええ、同じ問題があります。 http://stackoverflow.com/questions/43137955/image-gets-blurry-and-zoomed-out-when-erasing –

答えて

0

私はあなたのコードにバグを発見... uがこのライン を見てくださいすることができchosenImageは=インフォ[UIImagePickerControllerOriginalImage]としてましょう! UIImage この行は消去イメージを元のイメージに設定します。 このように変更してください。 selectedImage = info [UIImagePickerControllerEditedImage] as! UIImage は、私はこれが正常に動作します願っています... もuがこのラインpicker.allowsEditingを見ることができます= falseを// 2 この行は次のようでなければなりません**** picker.allowsEditing =真** // 2 * * これは間違いだと思います。

+0

私はこれをしますが、私の問題は解決されません。画像を拡大/縮小した後に消去すると、画像がぼやけて歪んでしまいます。解決策があれば教えてください –

関連する問題