Swift - Cropping images *outside* of allowsEditing -
i have very simple project set allows click "browse photo" button. user selects photo photo gallery, , it's displayed on programmatically created uiimageview.
works charm. - missing key functionality required.
i need user able scale image (via pinching , dragging) after displayed within uiimageview. allowsediting = true, lets user crop before. need similar functionality, however, allowing them edit once it's on main ui.
help appreciated. please , thank you!!
import uikit
class viewcontroller: uiviewcontroller {
@iboutlet weak var imageview: uiimageview! var imageviewlayer: calayer{ return imageview.layer } override func viewdidload() { super.viewdidload() // additional setup after loading view, typically nib. imageviewlayer.contents = uiimage(named: "ss3.jpg")?.cgimage imageviewlayer.contentsgravity = kcagravityresizeaspect } override func didreceivememorywarning() { super.didreceivememorywarning() // dispose of resources can recreated. } @ibaction func newgesture(sender: anyobject) { imageviewlayer.transform = catransform3dmakescale(sender.scale, sender.scale, 1) }
}
i did similar while back. added image uiimageview's layer property, added gesture recognizer view , implemented gesture call backs modifying layer property , not view. adding image uiimageview's layer did trick. side note, add every uiview supported calayer class. has lot of methods , properties dynamically change view, in case done gestures.
as alternative, can use calayer's hittest method instead of implementing call backs gesture recognizers.
edit- sample code
you thing this:
@iboutlet weak var imageview: uiimageview! var imageviewlayer: calayer{ return imageview.layer }
in viewdidload, set image
imageviewlayer.contents = uiimage(named: "coredatademoapp")?.cgimage imageviewlayer.contentsgravity = kcagravityresizeaspect
add pinch gesture imageview in storyboard (or programmatically) , in it's call this:
@ibaction func pinchgesturerecognized(sender: anyobject) { imageviewlayer.transform = catransform3dmakescale(sender.scale, sender.scale, 1) }
again give idea of how work , not complete code. hope helps!
this way of doing it:
Comments
Post a Comment