ios - Drawing on an image causes the photo to expand -
hi build in app allow users use finger draw line on top of image, issue when user touches screen start drawing images expands make image distorted.
here code of viewcontroller:
update: code updated try use second image view suggestion maile
import uikit import mobilecoreservices class newautographviewcontroler: uiviewcontroller, uinavigationcontrollerdelegate, uiimagepickercontrollerdelegate, uigesturerecognizerdelegate { var signeditmode: bool = false @iboutlet var btnedit: uibarbuttonitem! @iboutlet var textfieldalbum: uitextfield! @iboutlet var imageview: uiimageview! @iboutlet var signimageview: uiimageview! @ibaction func btnbackclick(sender: anyobject) { self.navigationcontroller?.popviewcontrolleranimated(true) } @ibaction func btneditclick(sender: anyobject) { if signeditmode { signeditmode = false signimageview.backgroundcolor = nil btnedit.title = "edit" signimageview.gesturerecognizers?.removeall() } else { signeditmode = true btnedit.title = "done" signimageview.backgroundcolor = uicolor(white: 1, alpha: 0.5) let tap = uipangesturerecognizer(target: self, action: selector("handletap:")) tap.delegate = self signimageview.userinteractionenabled = true signimageview.addgesturerecognizer(tap) } } func handletap(sender: uipangesturerecognizer? = nil) { let recognizer:uipangesturerecognizer = sender! let translation = recognizer.translationinview(self.view) if let view = recognizer.view { view.center = cgpoint(x:view.center.x + translation.x, y:view.center.y + translation.y) } recognizer.settranslation(cgpointzero, inview: self.view) } func getsaveresponse( response: bool){ dispatch_async(dispatch_get_main_queue()) { print("getsaveresponse") print(response) if response { let alert = uialertcontroller(title: "save", message: "autograph saved!", preferredstyle: .alert) let save = uialertaction(title: "new", style: .default) { (alertaction: uialertaction!) -> void in self.newphoto() } let done = uialertaction(title: "done", style: .default) { (alertaction: uialertaction!) -> void in self.btnbackclick(self) } alert.addaction(save) alert.addaction(done) self.presentviewcontroller(alert, animated: true, completion: nil) } else { let alert = uialertcontroller(title: "save", message: "uh oh! autograph not save!", preferredstyle: .alert) alert.addaction(uialertaction(title: "ok", style: .default, handler: nil)) self.presentviewcontroller(alert, animated: true, completion: nil) } } } @ibaction func btnsaveclick(sender: anyobject) { print("save click") mergeimages() let newphotoalbum = photoalbum() let validate = newphotoalbum.saveimage(imageview.image!, funparam: getsaveresponse) if validate == false { getsaveresponse(false) } } //drawing variables var lastpoint = cgpoint.zero var red: cgfloat = 0.0 var green: cgfloat = 0.0 var blue: cgfloat = 0.0 var brushwidth: cgfloat = 10.0 var opacity: cgfloat = 1.0 var swiped = false var imagesize: cgsize = cgsize(width: 0,height: 0) //end-drawing variables override func viewdidload() { super.viewdidload() newphoto() // additional setup after loading view. } override func didreceivememorywarning() { super.didreceivememorywarning() // dispose of resources can recreated. } func newphoto() { if (uiimagepickercontroller.issourcetypeavailable(uiimagepickercontrollersourcetype.camera)){ let picker = uiimagepickercontroller() picker.delegate = self picker.sourcetype = uiimagepickercontrollersourcetype.camera //picker.mediatypes = [kuttypeimage string] //picker.allowsediting = true self.presentviewcontroller(picker, animated: true, completion: nil) } else { let alertcontroller = uialertcontroller(title: "camera error", message: "no camera detected!", preferredstyle: uialertcontrollerstyle.alert) //alertcontroller.addaction(uialertaction(title: "dismiss", style: uialertactionstyle.default,handler: nil)) let dismiss = uialertaction(title: "dismiss", style: uialertactionstyle.default) { (alertaction: uialertaction!) -> void in self.navigationcontroller?.popviewcontrolleranimated(true) } alertcontroller.addaction(dismiss) self.presentviewcontroller(alertcontroller, animated: true, completion: nil) } } func imagepickercontroller(picker: uiimagepickercontroller, didfinishpickingmediawithinfo info: [string : anyobject]){ print("received image camera") //let mediatype = info[uiimagepickercontrollermediatype] as! string var originalimage:uiimage? //, editedimage:uiimage?, imagetosave:uiimage? //let compresult:cfcomparisonresult = cfstringcompare(mediatype nsstring!, kuttypeimage, cfstringcompareflags.comparecaseinsensitive) //if ( compresult == cfcomparisonresult.compareequalto ) { //editedimage = info[uiimagepickercontrollereditedimage] as! uiimage? originalimage = info[uiimagepickercontrolleroriginalimage] as! uiimage? /*if ( editedimage != nil ) { imagetosave = editedimage } else { imagetosave = originalimage } imageview.image = imagetosave imageview.reloadinputviews() }*/ imageview.image = originalimage picker.dismissviewcontrolleranimated(true, completion: nil) imagesize = (originalimage?.size)! //print(imageview.frame.size) } func imagepickercontrollerdidcancel(picker: uiimagepickercontroller) { picker.dismissviewcontrolleranimated(true, completion: nil) } /* // mark: - navigation // in storyboard-based application, want little preparation before navigation override func prepareforsegue(segue: uistoryboardsegue, sender: anyobject?) { // new view controller using segue.destinationviewcontroller. // pass selected object new view controller. } */ //drawing-functions override func touchesbegan(touches: set<uitouch>, withevent event: uievent?) { if signeditmode { return } swiped = false if let touch:uitouch = touches.first! uitouch { //lastpoint = touch.locationinview(self.view) lastpoint = touch.locationinview(signimageview) } } func drawlinefrom(frompoint: cgpoint, topoint: cgpoint) { /*var scale: cgfloat = 1.0; if uiscreen.mainscreen().respondstoselector(selector("scale")) { let tmp: cgfloat = uiscreen.mainscreen().scale if (tmp > 1.5) { scale = 2.0; } } if(scale > 1.5) { uigraphicsbeginimagecontextwithoptions(view.frame.size, false, scale); } else { uigraphicsbeginimagecontext(view.frame.size); } print(imageview.frame.size) */ // 1 //uigraphicsbeginimagecontext(view.frame.size) //uigraphicsbeginimagecontext(self.imageview.frame.size) uigraphicsbeginimagecontextwithoptions(self.signimageview.bounds.size, false, 0) let context = uigraphicsgetcurrentcontext() //tempimageview.image?.drawinrect(cgrect(x: 0, y: 0, width: view.frame.size.width, height: view.frame.size.height)) //imageview.image?.drawinrect(cgrect(x: 0, y: 0, width: view.frame.size.width, height: view.frame.size.height)) signimageview.image?.drawinrect(cgrect(x: 0, y: 0, width: self.signimageview.bounds.size.width, height: self.signimageview.bounds.size.height)) // 2 cgcontextmovetopoint(context, frompoint.x, frompoint.y) cgcontextaddlinetopoint(context, topoint.x, topoint.y) // 3 cgcontextsetlinecap(context, .round) cgcontextsetlinewidth(context, brushwidth) cgcontextsetrgbstrokecolor(context, red, green, blue, 1.0) cgcontextsetblendmode(context, .normal) // 4 cgcontextstrokepath(context) // 5 //tempimageview.image = uigraphicsgetimagefromcurrentimagecontext() //tempimageview.alpha = opacity //imageview.autoresizingmask = [.flexiblerightmargin, .flexibleleftmargin, .flexiblebottommargin, .flexibletopmargin] //change fit fill //imageview.contentmode = uiviewcontentmode.scaleaspectfill signimageview.image = uigraphicsgetimagefromcurrentimagecontext() uigraphicsendimagecontext() } override func touchesmoved(touches: set<uitouch>, withevent event: uievent?) { // 6 swiped = true if let touch:uitouch = touches.first! uitouch { //let currentpoint = touch.locationinview(view) let currentpoint = touch.locationinview(signimageview) drawlinefrom(lastpoint, topoint: currentpoint) // 7 lastpoint = currentpoint } } override func touchesended(touches: set<uitouch>, withevent event: uievent?) { if signeditmode { return } if !swiped { // draw single point drawlinefrom(lastpoint, topoint: lastpoint) } // merge tempimageview mainimageview /*uigraphicsbeginimagecontext(imageview.frame.size) imageview.image?.drawinrect(cgrect(x: 0, y: 0, width: view.frame.size.width, height: view.frame.size.height), blendmode: .normal, alpha: 1.0) imageview.image?.drawinrect(cgrect(x: 0, y: 0, width: view.frame.size.width, height: view.frame.size.height), blendmode: .normal, alpha: opacity) imageview.image = uigraphicsgetimagefromcurrentimagecontext() uigraphicsendimagecontext() tempimageview.image = nil*/ } //end-drawing-functions //to merge sign , image func mergeimages(){ uigraphicsbeginimagecontextwithoptions(imageview.bounds.size, false, 0) imageview.image?.drawinrect(cgrect(x: 0, y: 0, width: imageview.bounds.size.width, height: imageview.bounds.size.height), blendmode: .normal, alpha: 1.0) signimageview.image?.drawinrect(cgrect(x: 0, y: 0, width: imageview.bounds.size.width, height: imageview.bounds.size.height), blendmode: .normal, alpha: opacity) //added contentmode imageview.contentmode = uiviewcontentmode.scaleaspectfill imageview.image = uigraphicsgetimagefromcurrentimagecontext() uigraphicsendimagecontext() signimageview.image = nil } }
may ask wrong code?
problem may occur when device supported retina or uiscreen.mainscreen().nativescale > 1 try
uigraphicsbeginimagecontextwithoptions(self.imageview.bounds.size, false, 0.0)
instead of uigraphicsbeginimagecontext(self.imageview.frame.size)
Comments
Post a Comment