ios - My ImageView's content mode stop working after I draw something on the image -
thanks time.
the image fine before tap image, , image compacted after tap it.
here code:
i not using storyboard, create code, here imageview. , added constraints code well.
let imageeditingview: uiimageview = { let imageview = uiimageview() imageview.contentmode = .scaleaspectfill imageview.clipstobounds = true return imageview }() override func touchesbegan(_ touches: set<uitouch>, event: uievent?) { if touches.first != nil { lastpoint = (touches.first?.location(in: imageeditingview))! } } override func touchesmoved(_ touches: set<uitouch>, event: uievent?) { if touches.first != nil { let currentpoint = touches.first?.location(in: imageeditingview) drawlines(frompoint: lastpoint, topoint: currentpoint!) lastpoint = currentpoint! drawlines(frompoint: lastpoint, topoint: lastpoint) } } func drawlines(frompoint: cgpoint, topoint: cgpoint) { uigraphicsbeginimagecontext(imageeditingview.frame.size) imageeditingview.image?.draw(in: cgrect(x: 0, y: 0, width: imageeditingview.frame.width, height: imageeditingview.frame.height)) let context = uigraphicsgetcurrentcontext() context?.move(to: cgpoint(x: frompoint.x, y: frompoint.y)) context?.addline(to: cgpoint(x: topoint.x, y: topoint.y)) context?.setblendmode(cgblendmode.normal) context?.setlinecap(cglinecap.round) context?.setlinewidth(cgfloat(int(120 * linewidthsliderview.value))) context?.setstrokecolor(red: red / 255, green: green / 255, blue: blue / 255, alpha: 0.01) context?.strokepath() imageeditingview.image = uigraphicsgetimagefromcurrentimagecontext() uigraphicsendimagecontext() }
i not know mean "compacted", guessing image gets corrupted in way because data lost when convert image cgcontext
, image.
i don't know how fix this, work around adding cashapelayer
sublayer uiimageview
, , drawing want there in form of cgpath
. syntax similar using right now, , effect might not supported blend mode, might able recreate layer lower alpha value.
here like.
var drawinglayer = cashapelayer() func drawlines(frompoint: cgpoint, topoint: cgpoint) { let mutable = cgmutablepath() mutable.move(to: frompoint) mutable.addline(to: topoint) drawinglayer.path = mutable drawinglayer.fillcolor = nil drawinglayer.linecap = kcalinecapround drawinglayer.linecap = 120 * cgfloat(linewidthsliderview.value) //the bit in code translated whole thing int before translating //cgfloat, bad idea since ints cannot store decimals, if there no //direct conversion, convert double or float drawinglayer.strokecolor = uicolor(calibratedred: red/255, green: green/255, blue: blue/255 , alpha: 1).cgcolor }
somewhere need imageeditingview.addsublayer(drawinglayer)
also, don't need convert cgpoint cgpoint, when context.move(to:)
, other places...
Comments
Post a Comment