繁体   English   中英

删除缩放的图像iOS。

[英]Erasing zoomed image 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

        }
    }

}

我在您的代码中发现了该错误...您能否看一下这一行, 让selectedImage = info [UIImagePickerControllerOriginalImage]设置为! UIImage这行会导致将擦除图像设置为原始图像,您应该像这样更改此行, 让selectedImage = info [UIImagePickerControllerEditedImage]为! UIImage我希望这行得通...也可以看到这行选择器.allowsEditing = false // 2这行必须像这样**** picker.allowsEditing = true ** // 2 **我猜这是错误

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM