swift - Drawing on UIImage in UIScrollView -


the problem

this going sound crazy. i'm making drawing app , want users able draw on images bigger or smaller screen. when user selects image photo library put image view in scroll view. user draws on image views same dimensions selected image , in scroll view on top of other one. scrolling of 2 scroll views synchronized when draw scroll drawing appears above image (in right place). reason however, when user selects long image (let's 400 x 2000), drawing works @ top of image, when scroll down draw, lines draw go top. can't figure out what's going wrong... code below.

about code

camerastill image view containing image

drawable height of image

myscroll scroll view image

mainimageview, tempimageview, undo1, undo2, undo3 drawing layers

drawscroll scroll view drawing layers

image selection

func imagepickercontroller(picker: uiimagepickercontroller, didfinishpickingimage image: uiimage!, editinginfo: [nsobject : anyobject]!) {         self.dismissviewcontrolleranimated(true, completion: { () -> void in          })          if (image != nil) { self.camerastill.contentmode = uiviewcontentmode.scaleaspectfit             camerastill.frame = cgrectmake(0, 0, screenwidth, screenwidth*(image.size.height/image.size.width))              // change uiimageivews size              mainimageview.frame = cgrectmake(0, 0, screenwidth, screenwidth*(image.size.height/image.size.width))             tempimageview.frame = cgrectmake(0, 0, screenwidth, screenwidth*(image.size.height/image.size.width))             undo1.frame = cgrectmake(0, 0, screenwidth, screenwidth*(image.size.height/image.size.width))             undo2.frame = cgrectmake(0, 0, screenwidth, screenwidth*(image.size.height/image.size.width))             undo3.frame = cgrectmake(0, 0, screenwidth, screenwidth*(image.size.height/image.size.width))              drawable = screenwidth*(image.size.height/image.size.width)              myscroll.contentsize = cgsize(width: screenwidth,height: screenwidth*(image.size.height/image.size.width))             drawscroll.contentsize = cgsize(width: screenwidth,height: screenwidth*(image.size.height/image.size.width))              if (screenwidth*(image.size.height/image.size.width) > (screenheight-130)) {                 myscroll.scrollenabled = true                 drawscroll.scrollenabled = true             }             else {                 myscroll.scrollenabled = false                 drawscroll.scrollenabled = false                 camerastill.center = cgpoint(x: screenwidth/2, y: (screenheight-130)/2)                 mainimageview.center = cgpoint(x: screenwidth/2, y: (screenheight-130)/2)                 tempimageview.center = cgpoint(x: screenwidth/2, y: (screenheight-130)/2)                 undo1.center = cgpoint(x: screenwidth/2, y: (screenheight-130)/2)                 undo2.center = cgpoint(x: screenwidth/2, y: (screenheight-130)/2)                 undo3.center = cgpoint(x: screenwidth/2, y: (screenheight-130)/2)             }               self.camera!.stopcamera()         }           //drawview.alpha = 1.0      } 

drawing

override func touchesbegan(touches: set<nsobject>, withevent event: uievent) {         println("began")          if (drawingenabled == true) {             c1 = 3             closeallextras()             swiped = false             if let touch = touches.first as? uitouch {                 lastpoint = touch.locationinview(self.view)             }         }       }        func drawlinefrom(frompoint: cgpoint, topoint: cgpoint) {          //if (frompoint.y > 50 && frompoint.y < screenheight-80 && topoint.y > 50 && topoint.y < screenheight-80) {             // 1             uigraphicsbeginimagecontext(cgsize(width: view.frame.size.width,height: drawable))             let context = uigraphicsgetcurrentcontext()             tempimageview.image?.drawinrect(cgrect(x: 0, y: 0, width: view.frame.size.width, height: drawable))              // 2             cgcontextmovetopoint(context, frompoint.x, frompoint.y)             cgcontextaddlinetopoint(context, topoint.x, topoint.y)              // 3             cgcontextsetlinecap(context, kcglinecapround)             cgcontextsetlinewidth(context, brushwidth)             cgcontextsetrgbstrokecolor(context, red, green, blue, 1.0)             cgcontextsetblendmode(context, kcgblendmodenormal)              // 4             cgcontextstrokepath(context)              // 5             tempimageview.image = uigraphicsgetimagefromcurrentimagecontext()             tempimageview.alpha = opacity             uigraphicsendimagecontext()         //}        }      override func touchesmoved(touches: set<nsobject>, withevent event: uievent) {         // 6          if (drawingenabled == true) {             swiped = true             if let touch = touches.first as? uitouch {                 let currentpoint = touch.locationinview(view)                 drawlinefrom(lastpoint, topoint: currentpoint)                  // 7                 lastpoint = currentpoint             }         }      }      func mergeviewcontext(v1 : uiimageview, v2: uiimageview) {         uigraphicsbeginimagecontext(v1.frame.size)         v1.image?.drawinrect(cgrect(x: 0, y: 0, width: view.frame.size.width, height: drawable), blendmode: kcgblendmodenormal, alpha: 1.0)         v2.image?.drawinrect(cgrect(x: 0, y: 0, width: view.frame.size.width, height: drawable), blendmode: kcgblendmodenormal, alpha: 1.0)         v1.image = uigraphicsgetimagefromcurrentimagecontext()         uigraphicsendimagecontext()          v2.image = nil     }      override func touchesended(touches: set<nsobject>, withevent event: uievent) {          if (drawingenabled == true) {             if !swiped {                 // draw single point                 drawlinefrom(lastpoint, topoint: lastpoint)             }             mergeviewcontext(mainimageview, v2: undo1)              undo1.image = undo2.image             undo2.image = nil             undo2.image = undo3.image             undo3.image = nil               uigraphicsbeginimagecontext(undo3.frame.size)             undo3.image?.drawinrect(cgrect(x: 0, y: 0, width: view.frame.size.width, height: drawable), blendmode: kcgblendmodenormal, alpha: 1.0)             tempimageview.image?.drawinrect(cgrect(x: 0, y: 0, width: view.frame.size.width, height: drawable), blendmode: kcgblendmodenormal, alpha: opacity)             undo3.image = uigraphicsgetimagefromcurrentimagecontext()             uigraphicsendimagecontext()              tempimageview.image = nil         } 

synching both scroll views

func scrollviewdidscroll(scrollview: uiscrollview) {         if (scrollview == drawscroll) {             var offset = scrollview.contentoffset             myscroll.setcontentoffset(offset, animated: false)         }     } 

i got work correcting following values offset of scroll view. however, blurring long images , strange bug short ones. no idea what's wrong.

cgcontextmovetopoint(context, frompoint.x, frompoint.y) cgcontextaddlinetopoint(context, topoint.x, topoint.y) 

Comments

Popular posts from this blog

angularjs - ADAL JS Angular- WebAPI add a new role claim to the token -

php - CakePHP HttpSockets send array of paramms -

node.js - Using Node without global install -