ios - Adjusting image size on imagView and left bar button item -
in project have 'change logo' option. on clicking gray logo in front of change logo, gallery opens , user chooses photo it. done using following code:
-(void)imagepickercontroller:(uiimagepickercontroller *)picker didfinishpickingmediawithinfo:(nsdictionary *)info{ uiimage *image = info[uiimagepickercontrolleroriginalimage]; [imagepicker dismissviewcontrolleranimated:yes completion:nil]; imgtobesaved = image; self.imgviewchooselogo.contentmode = uiviewcontentmodescaleaspectfit; self.imgviewchooselogo.image = image; imagechanged = yes; }
now image saved in documents directory. image should serve logo view controllers. have set left bar button item using following code:
nsdata *imagedata = [nsdata datawithcontentsoffile:[appdelegate documentdirectorywithfilepath]]; uiimage *image = [uiimage imagewithdata:imagedata]; uiimage *new = [appdelegate imagewithimage:image scaledtosize:cgsizemake(150, 40)]; uiimage *new1 = [new imagewithrenderingmode:uiimagerenderingmodealwaysoriginal]; uibarbuttonitem *barbuttonitem = [[uibarbuttonitem alloc] initwithimage:new1 style:uibarbuttonitemstyleplain target:nil action:nil]; [self.navigationitem setleftbarbuttonitem:barbuttonitem];
where method scaledtosize goes as
+(uiimage*)imagewithimage:(uiimage*)image scaledtosize:(cgsize)newsize; { uigraphicsbeginimagecontext( newsize ); [image drawinrect:cgrectmake(0,0,newsize.width,newsize.height)]; uiimage* newimage = uigraphicsgetimagefromcurrentimagecontext(); uigraphicsendimagecontext(); return newimage; }
i have used 150*40 default logo fits perfectly. problem comes when have use other image choosen gallery. stretches on bar button item. don't know how set ratio of height , width of choosen gallery image image does'nt weired on left bar button item.
you should let user crop image before use it. have @ these frameworks (if don't want implement yourself:
Comments
Post a Comment