ios - How do you save the combination of an image and lines drawn on it? Swift -


i've got saving image down, whenever draw lines on image , try save image, image doesn't save lines drew on it. how go saving images lines drawn on it?

here's current code drawing lines:

import uikit import coredata  class drawclass: uiview {     /*     // override drawrect: if perform custom drawing.     // empty implementation adversely affects performance during animation.     override func drawrect(rect: cgrect) {         // drawing code     }     */     ///creating array of type line accepts line object. 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)         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)     } } 

update sorry, forgot add in saving method. way "earth" name of image placed in storyboard here is:

@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 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)     } } 

change current saveimage method implementation.

@ibaction func saveimage(sender: uibutton) {       uiimagewritetosavedphotosalbum(earth.image!, self, "image:didfinishsavingwitherror:contextinfo:", nil)  } 

all explicitly save image of uiimageview instance earth.

to save them lines, need create new image earth view, follows:

     @ibaction func saveimage(sender: uibutton) {              uigraphicsbeginimagecontextwithoptions(earth.frame.size, earth.opaque, 0.0)             earth.layer.renderincontext(uigraphicsgetcurrentcontext()!)             let imagewithlines = uigraphicsgetimagefromcurrentimagecontext()             uigraphicsendimagecontext()              // have image save             uiimagewritetosavedphotosalbum(imagewithlines, self, selector("savecomplete"), nil)      }       func savecomplete () {          nslog("complete!");      } 

Comments