i want write function in swift takes image , crops out except thin horizontal in middle. don't want preserve aspect ratio.
this have far doesn't work way want to. want preserve pixels y=276 y=299.
func cropimagetobars(image: uiimage) -> uiimage { let rect = cgrectmake(0, 200, image.size.width, 23) uigraphicsbeginimagecontextwithoptions(rect.size, false, 1.0) image.drawinrect(rect) let newimage = uigraphicsgetimagefromcurrentimagecontext() uigraphicsendimagecontext() return newimage
}
your code backwards. uiimage drawinrect() method draws whole image target rectangle. think of film enlarger or slide projector. adjust size , shape of box renders image into. set image context that's piece of film captures bit of image.
you typically want render image rectangle that's full size of image, it's origin shifted image context origin @ 0 captures desired bit of image.
i have demo project called cropimg shows how crop portions of image. has ui lets user select section of image. case simpler, should give idea.
Comments
Post a Comment