如何保存图像和在其上绘制的线条的组合?迅速

时间:2016-04-06 14:17:20

标签: ios swift image

我已经保存了图像,但每当我在图像上画线并尝试保存图像时,图像就不会用我画的线条保存。如何使用绘制的线条保存图像?

这是绘制线的当前代码:

import UIKit
import CoreData

class DrawClass: UIView {
    /*
    // Only override drawRect: if you perform custom drawing.
    // An empty implementation adversely affects performance during animation.
    override func drawRect(rect: CGRect) {
        // Drawing code
    }
    */
    ///Creating an array of type Line which accepts a Line object. It is an empty array.
    var lines:[Line] = []
    var lastPoint: CGPoint!

    required init(coder aDecoder: NSCoder)
    {
        super.init(coder: aDecoder)
        self.backgroundColor = UIColor.whiteColor()
        self.layer.zPosition = 1
    }

    override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent){
        if let touch = touches.first as? UITouch {
            lastPoint =  touch.locationInView(self)
        }
    }

    override func touchesMoved(touches: Set<NSObject>, withEvent event: UIEvent){
        if let touch = touches.first as? UITouch {
            var newPoint = touch.locationInView(self)
            lines.append(Line(start: lastPoint, end: newPoint))
            lastPoint = newPoint
        }
        self.setNeedsDisplay()
    }

    override func drawRect(rect: CGRect) {
        var context = UIGraphicsGetCurrentContext()
        CGContextBeginPath(context)
        for line in lines
        {
            CGContextMoveToPoint(context, line.start.x, line.start.y)
            CGContextAddLineToPoint(context, line.end.x, line.end.y)
        }

        var storage: Float = ViewController.simple.storage1
        var storage2: Float = ViewController.simple.storage2
        var storage3: Float = ViewController.simple.storage3

        CGContextSetRGBStrokeColor(context, CGFloat(storage), CGFloat(storage2), CGFloat(storage3), 1)

        CGContextSetLineWidth(context, 5)
        CGContextStrokePath(context)
    }
}

更新 对不起,我忘了添加保存方法。顺便说一句&#34; earth&#34;是我放在故事板中的图像的名称这是:

@IBAction func saveImage(sender: UIButton) {


    UIImageWriteToSavedPhotosAlbum(earth.image!, self, "image:didFinishSavingWithError:contextInfo:", nil)

}

func image(image: UIImage, didFinishSavingWithError error: NSError?, contextInfo:UnsafePointer<Void>) {
    if error == nil {
        let ac = UIAlertController(title: "Saved!", message: "Your altered image has been saved to your photos.", preferredStyle: .Alert)
        ac.addAction(UIAlertAction(title: "OK", style: .Default, handler: nil))
        presentViewController(ac, animated: true, completion: nil)
    } else {
        let ac = UIAlertController(title: "Save error", message: error?.localizedDescription, preferredStyle: .Alert)
        ac.addAction(UIAlertAction(title: "OK", style: .Default, handler: nil))
        presentViewController(ac, animated: true, completion: nil)
    }
}

2 个答案:

答案 0 :(得分:0)

绘制线条后,您可以保存从当前上下文中获取的UIImage

UIGraphicsBeginImageContext(view.bounds.size);
[view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *imageFromView = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

答案 1 :(得分:0)

很难说只有这段代码片段而没有你的故事板,但我会尝试:

将UIImageView放在故事板中的DrawClass中并为其创建一个出口。然后处理在初始化中设置图像。这样当你保存时你可以快照DrawClass(图像和线条)

您目前是如何尝试保存图片的?

编辑:

因此,不是在UIImageView中拖动并设置插座,一旦设置了UIImage,以编程方式创建UIImageView会更容易。

所以在你的DrawClass实现中:

var image: UIImage! {
    didSet{
       imageView = UIImageView(frame: self.bounds)
       imageView.image = image
       self.addSubView(imageView)
       self.setNeedsDisplay()
    }
}

var imageView: UIImageView!

然后在viewDidLoad中的主viewController中放置:

drawCanvas.image = UIImage(named: "yourImage")

在DrawClass视图中加载图像后,您可以使用Vinod的方法来保存图像。 我建议将保存图像代码放在DrawClass中,然后你有一个完全可重用的类。所以我认为在DrawClass中添加一个函数:

func saveImage() {
    //Vinod's code to save
    UIGraphicsBeginImageContextWithOptions(self.frame.size, true, 0.0)
        self.layer.renderInContext(UIGraphicsGetCurrentContext()!)
        let imageWithLines = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()

        // NOW you have the image to save
        UIImageWriteToSavedPhotosAlbum(imageWithLines, self, Selector("saveComplete"), nil)
 }

 func saveComplete () {
     NSLog("Complete!");
 }

然后在saveImage func中的主视图控制器中:

@IBAction func saveImage(sender: UIButton) {
        drawClass.saveImage()
}