c# - iOS Fit Image to Screen -
i have image 646x289 trying fit in screen respect aspect ratio.
here current approach:
controller:
public override void viewdidlayoutsubviews() { base.viewdidlayoutsubviews(); _imnlogo.contentmode = uiviewcontentmode.scaleaspectfit; _imnlogo.sizetofit(); _imnlogo.frame = new cgrect( view.bounds.left + 2 * globals.margingrid, view.bounds.top + globals.margingrid, _scaledimage.size.width, _scaledimage.size.height); } public override void loadview() { base.loadview(); _scaledimage = maxresizeimage( uiimage.fromfile("imn_logo.png"), (float) view.bounds.width, (float) view.bounds.height); _imnlogo = new uiimageview(_scaledimage); view.addsubview(_imnlogo); } public uiimage maxresizeimage(uiimage sourceimage, float maxwidth, float maxheight) { var sourcesize = sourceimage.size; var maxresizefactor = math.max(maxwidth / sourcesize.width, maxheight / sourcesize.height); if (maxresizefactor > 1) return sourceimage; var width = maxresizefactor * sourcesize.width; var height = maxresizefactor * sourcesize.height; uigraphics.beginimagecontext(new sizef((float) width, (float) height)); sourceimage.draw(new rectanglef(0, 0, (float) width, (float) height)); var resultimage = uigraphics.getimagefromcurrentimagecontext(); uigraphics.endimagecontext(); return resultimage; }
when loads, image way large , doesn't fit in screen.
i constructing of interfaces in c# (using xamarin) need able using frames , bounds.
use contentmode
of uiimageview
control how image scaled , can skip manual resizing:
public override void loadview() { base.loadview(); _imnlogo = new uiimageview(uiimage.fromfile("imn_logo.png")); _imnlogo.frame = view.frame; _imnlogo.contentmode = uiviewcontentmode.scaleaspectfill; view.addsubview(imageview); view.sendsubviewtoback(imnlogo); // if want place other `view`s on top of logo... }
ref: https://developer.apple.com/reference/uikit/uiviewcontentmode
Comments
Post a Comment