删除缩放图像iOS。

时间:2016-08-30 10:30:30

标签: ios swift image-processing core-graphics

当我擦除图像时,它正常工作。但是在放大/缩小图像后,当我尝试擦除时,图像会被擦除但同时会缩小。我做错了什么? 这是我的代码。

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

        }
    }

}

1 个答案:

答案 0 :(得分:0)

我在你的代码中发现了这个错误......请你看看这一行 让selectedImage = info [UIImagePickerControllerOriginalImage]为!的UIImage 此行会导致擦除图像设置为原始图像 像这样改变这条线 让selectedImage = info [UIImagePickerControllerEditedImage]为!的UIImage 我希望这很好...... 也可以看到 picker.allowsEditing = false // 2 这一行 这一行必须像这样**** picker.allowsEditing = true ** // 2 ** 我猜这是错误