ios - AVMakeRectWithAspectRatioInsideRect returning correct image but wrong size -
i new ios swift development newer manipulating images. have uiimageview inside of uiscrollview. want user zoom/pan show exact image want capture. have working extent. when user clicks button, capture image , in uiscrollview box, however, size of image not correct. appreciated!!
@ibaction func tapsendtoband(sender: uibutton) { var frameinfo = frameforimage(photoimageview.image!, imageview: photoimageview) var imagewidth = photoimageview.image!.size.height var scale:cgfloat = cgfloat(1.0) / (photoimageview.frame.size.height / imagewidth) var visiblerect = cgrect() var pwidth = cgfloat(cgimagegetwidth(photoimageview.image?.cgimage)) var pheight = cgfloat(cgimagegetheight(photoimageview.image?.cgimage)) visiblerect.origin = scrollview.contentoffset visiblerect.size = scrollview.bounds.size visiblerect.origin.x *= scale visiblerect.origin.y *= scale visiblerect.size.width *= scale visiblerect.size.height *= scale var psize:cgsize = cgsize(width: visiblerect.size.width,height: visiblerect.size.height) let brect = avmakerectwithaspectratioinsiderect(psize, visiblerect) uiimagewritetosavedphotosalbum(cropimage(photoimageview.image!, rect: brect), nil, nil, nil) phototest.image = cropimage(photoimageview.image!, rect: brect) }
here frame function using try determine sizing
func frameforimage (image:uiimage, imageview:uiimageview) -> cgrect{ var imageratio = image.size.width / image.size.height; var viewratio = imageview.frame.size.width / imageview.frame.size.height; if(imageratio < viewratio){ var scale = imageview.frame.size.height / image.size.height; var width = scale * image.size.width; var topleftx = (imageview.frame.size.width - width) * 0.5; return cgrectmake(topleftx, 0, width, imageview.frame.size.height) } else{ var scale = imageview.frame.size.width / image.size.width; var height = scale * image.size.height; var toplefty = (imageview.frame.size.height - height) * 0.5; return cgrectmake(0, toplefty, imageview.frame.size.width, height); } }
here function use crop image
func cropimage(srcimage:uiimage,rect:cgrect) -> uiimage { var cgimageconv = srcimage.cgimage var cgsizeconv:cgsize = cgsize(width: 310,height: 102) var cr:cgimageref = cgimagecreatewithimageinrect(cgimageconv, rect) var cropped:uiimage = uiimage(cgimage: cr)! return cropped }
the code below sets mode image view aspect ratio , fill area of screen.
//makes image view fill real estate have available on phones screen youruiimageview.autoresizingmask = uiviewautoresizing.flexiblebottommargin | uiviewautoresizing.flexibleheight | uiviewautoresizing.flexiblerightmargin | uiviewautoresizing.flexibleleftmargin | uiviewautoresizing.flexibletopmargin | uiviewautoresizing.flexiblewidth
\
//will make image view aspect ratio youruiimageview.contentmode = uiviewcontentmode.scaleaspectfit
Comments
Post a Comment