【移动应用开发技术】iOS UIImageUIImageView处理_第1页
【移动应用开发技术】iOS UIImageUIImageView处理_第2页
【移动应用开发技术】iOS UIImageUIImageView处理_第3页
【移动应用开发技术】iOS UIImageUIImageView处理_第4页
【移动应用开发技术】iOS UIImageUIImageView处理_第5页
已阅读5页,还剩10页未读 继续免费阅读

付费下载

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

【移动应用开发技术】iOSUIImageUIImageView处理

今天处理了把iOS6版本改为iOS7的过程了,遇到了一些问题,查了一些资料,纪录一下:

1,iPad1上,更新图标以后最后先把原有程序卸载了,要不然图片残留很严重,还遇到一个问题说是调试过程中有其他进程在调试,重启iPad1就好了。就因为图片残留的问题,至少耽搁我1-2小时。

2,UINavigationBar自定义:看看支持到4.3的解法:#pragmamark-

#pragmamark自定义导航栏背景

@implementationUINavigationBar(CustomImage)

-(UIImage*)barBackground

{

UIImage*bg=[UIImagep_w_picpathNamed:@"bg_navbar_ios7.png"];

if([FMUSystemgetOSVersion]<7.0){

bg=[FMUImagep_w_picpathByScalingAndCroppingForSize:CGSizeMake(320,44)SourceImage:bgCropType:FMUIMAGE_SCALE_TYPE_FITMIN];

//UIGraphicsBeginImageContext(CGSizeMake(320,44));

//CGContextRefcontext=UIGraphicsGetCurrentContext();

//[bgdrawInRect:CGRectMake(0,0,320,44)];

//bg=UIGraphicsGetImageFromCurrentImageContext();

//UIGraphicsEndImageContext();

}

returnbg;

}

-(void)didMoveToSuperview

{

//iOS5only

if([selfrespondsToSelector:@selector(setBackgroundImage:forBarMetrics:)])

{

[selfsetBackgroundImage:[selfbarBackground]forBarMetrics:UIBarMetricsDefault];

}

}

//thisdoesn'tworkoniOS5butisneededforiOS4andearlier

-(void)drawRect:(CGRect)rect

{

//drawp_w_picpath

[[selfbarBackground]drawInRect:rect];

}

@end

为了支持iOS7,使用320*64的图片,还得我自己来裁一下。我个人比较喜欢注释掉的方法。上面的函数是其他同事实现的公有方法。

3,生成圆形图

3.1layer层画圆角UIImageView*p_w_picpathView=[[UIImageViewalloc]initWithImage:[UIImagep_w_picpathNamed:@"oiuyfdsa.png"]];

p_w_picpathView.frame=CGRectMake(20.f,20.f,100.f,100.f);

p_w_picpathView.layer.masksToBounds=YES;

p_w_picpathView.layer.cornerRadius=50;

3.2画布画-(UIImage*)circleImage:(UIImage*)p_w_picpathwithParam:(CGFloat)inset{

UIGraphicsBeginImageContext(p_w_picpath.size);

CGContextRefcontext=UIGraphicsGetCurrentContext();

CGContextSetLineWidth(context,2);

CGContextSetStrokeColorWithColor(context,[UIColorredColor].CGColor);

CGRectrect=CGRectMake(inset,inset,p_w_picpath.size.width-inset*2.0f,p_w_picpath.size.height-inset*2.0f);

CGContextAddEllipseInRect(context,rect);

CGContextClip(context);

[p_w_picpathdrawInRect:rect];

CGContextAddEllipseInRect(context,rect);

CGContextStrokePath(context);

UIImage*newimg=UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();

returnnewimg;

}

4,图片平铺的方式

4.1图片转换为uicolor,设为背景,有点像OpenGL中纹理设置。UIColor*circleColorPattern=[UIColorcolorWithPatternImage:

[UIImagep_w_picpathNamed:@"circle_pattern.png"]];

4.2图片转换为类似android中9.png的方式

iOS5之后:UIImage*buttonBackgroundImage=[[UIImagep_w_picpathNamed:@"button_bkg.png"]resizableImageWithCapInsets:UIEdgeInsetsMake(0,13,0,13)];

[buttonsetBackgroundImage:buttonBackgroundImageforState:UIControlStateNormal];

这是取27个像素,中间那个像素拉伸活者压缩。

iOS5之前的方法:-(UIImage*)stretchableImageWithLeftCapWidth:(NSInteger)leftCapWidth

topCapHeight:(NSInteger)topCapHeight;

这个方法有局限性,它只能指定leftCapWidth和topCapHeight,然后只有一个像素能够重复,也就是rightCapWidth为p_w_picpathWidth-leftCapWidth-1,而bottomCapHeight为p_w_picpathHeight-topCapHeight-1,所以重复的始终是中间的那一个像素.

最后摘录一份iOS6,7的适配文章,这文章已经看过2遍以上,可是需要的时候还要查,所以放在这里。Likemanyofyou,IhavebeenverybusyupgradingmyappstomakethemfitforiOS7.ThelatestversionofiOSintroduceslotsofvisualchanges.Fromadeveloper’sperspective,thenavigationbarandstatusbararetwonoticeablechangesthatneedtocater.Thestatusbarisnowtransparent,thatmeansthenavigationbarbehinditshowsthrough.Insomecases,thebackgroundp_w_picpathforanavigationbarcanextendupbehindthestatusbar.Sometimeago,I’vewrittenatutorialabouthowtocustomizeanavigationbar.Ithinkit’stimetorevisitthecustomizationandseehowitisdoneiniOS7.Herearesomeofthetipsandtricksthatyou’llfindinthisarticle:SettingthebackgroundcolorofnavigationbarUsingbackgroundp_w_picpathinnavigationbarCustomizingthecolorofbackbuttonChangingthefontofnavigationbartitleAddingmultiplebarbuttonitemsChangingthestyleofstatusbarHidingthestatusbarYou’llneedXcode5toproperlyexecutethecodeaspresentedinthistutorial.Soifyou’restillusingolderversionsofXcode,makesureyouupgradetoXcode5beforerunningthesampleXcodeproject.DefaultNavigationBariniOS7Beforewegointothecustomization,let’sfirsttakealookatthedefaultnavigationbargeneratedbyXcode5andiOS7.SimplycreateaXcodeprojectusingSingleViewControllertemplate.Embedtheviewcontrollerinanavigationcontroller.Ifyoudon’twanttostartfromscratch,youcanjustdownloadthissampleXcodeproject.Xcode5bundlesbothiOS6andiOS7Simulators.TrytorunthesampleprojectusingbothversionsofSimulators.Asyoucansee,thenavigationbariniOS7isbydefaultintertwinedwiththestatusbar.Thedefaultcolorisalsochangedtolightgray,aswell.ChangingtheBackgroundColorofNavigationBarIniOS7,thetintColorpropertyisnolongerusedforsettingthecolorofthebar.Instead,usethebarTintColorpropertytochangethebackgroundcolor.YoucaninsertthebelowcodeinthedidFinishLaunchingWithOptions:ofAppDelegate.m.Hereistheresult:Normallyyouwanttouseyourowncolorasthesystemcolordoesn’tlooknice.HereisaveryusefulmacroforsettingRGBcolor:SimplyputitsomewhereatthebeginningofAppDelegate.manduseittocreateanyUIColorobjectwithwhateverRGBcoloryouwant.Belowisanexample:Bydefault,thetranslucentpropertyofnavigationbarissettoYES.Additionally,thereisasystemblurappliedtoallnavigationbars.Underthissetting,iOS7tendstodesaturatethecolorofthebar.Herearethesamplenavigationbarswithdifferenttranslucentsetting.Todisablethetranslucentproperty,youcansimplyselectthenavigationbarinStoryboard.UnderAttributeInspectors,uncheckthetranslucentcheckbox.UsingBackgroundImageinNavigationBarIfyourappusesacustomp_w_picpathasthebackgroundofthebar,you’llneedtoprovidea“taller”p_w_picpathsothatitextendsupbehindthestatusbar.Theheightofnavigationbarischangedfrom44points(88pixels)to64points(128pixels).YoucanstillusethesetBackgroundImage:methodtoassignacustomp_w_picpathforthenavigationbar.Hereisthelineofcodeforsettingthebackgroundp_w_picpath:ThesampleXcodeprojectbundlestwodifferentbackgroundp_w_picpaths:nav_bg.pngandnav_bg_ios7.png.Trytotestthemout.ChangingtheFontofNavigationBarTitleJustlikeiOS6,youcancustomizethetextstylebyusingthe“titleTextAttributes”propertiesofthenavigationbar.Youcanspecifythefont,textcolor,textshadowcolor,andtextshadowoffsetforthetitleinthetextattributesdictionary,usingthefollowingtextattributekeys:UITextAttributeFont–KeytothefontUITextAttributeTextColor–KeytothetextcolorUITextAttributeTextShadowColor–KeytothetextshadowcolorUITextAttributeTextShadowOffset–KeytotheoffsetusedforthetextshadowHereisthesamplecodesnippetsforalteringthefontstyleofthenavigationbartitle:Ifyouapplythechangetothesampleapp,thetitleofnavigationbarshouldlooklikethis:CustomizingtheColorofBackbuttonIniOS7,allbarbuttonsareborderless.Thebackbuttonisnowachevronplusthetitleofthepreviousscreen(orjustdisplays‘Back’asthebuttontitleifthetitleofthepreviousscreenisnil).Totintthebackbutton,youcanalterthetintColorproperty,whichprovidesaquickandsimplewaytoskinyourappwithacustomcolor.Belowisasamplecodesnippet:Inadditiontothebackbutton,pleasenotethatthetintColorpropertyaffectsallbuttontitles,andbuttonp_w_picpaths.Ifyouwanttouseacustomp_w_picpathtoreplacethedefaultchevron,youcansetthebackIndicatorImageandbackIndicatorTransitionMaskImagetoyourp_w_picpath.Thecolorofthep_w_picpathiscontrolledbythetintColorproperty.UseImageasNavigationBarTitleDon’twanttodisplaythetitleofnavigationbarasplaintext?Youcanreplaceitwithanp_w_picpathoralogobyusingalineofcode:WesimplychangethetitleViewpropertyandassignitwithacustomp_w_picpath.ThisisnotanewfeatureiniOS7.ThecodealsoappliestolowerversionsofiOS.AddingMultipleBarButtonItemsAgain,thistipisnotspecificallyforiOS7.Butassomeofyouhaveraisedsuchquestionbefore,Idecidetoputthetipinthistutorial.Fromtimetotime,youwanttoaddmorethanonebarbuttonitemononesideofthenavigationbar.BoththeleftBarButtonItemsandrightBarButtonItemspropertiesletsyouassigncustombarbuttonitemsontheleft/rightsideofthenavigationbar.Say,youwanttoaddacameraandasharebuttonontherightsideofthebar.Youcanusethefollowingcode:Hereisthesampleresult:ChangingtheStyleofStatusBarInolderversionsofiOS,thestatusbarwasalwaysinblackstyleandthereisnotmuchyoucanchange.WiththereleaseofiOS7,you’reallowedtochangetheappearanceofthestatusbarperviewcontroller.YoucanuseaUIStatusBarStyleconstanttospecifywhetherthestatusbarcontentshouldbedarkorlightcontent.Bydefault,thestatusbardisplaysdarkcontent.Inotherwords,itemssuchastime,batteryindicatorandWi-Fisignalaredisplayedindarkcolor.Ifyou’reusingadarkbackgroundinnavigationbar,you’llendupwithsomethinglikethis:Inthiscase,youprobablyneedtochangethestyleofstatusbarfromdarktolight.Therearetwowaystodothis.IniOS7,youcancontrolthestyleofthestatusbarfromanindividualviewcontrollerbyoverridingthepreferredStatusBarStyle:Forthesampleapp,simplyputtheabovecodeintheRecipeNavigationController.mandthestatusbarwilldisplaylightcontent.ThemethodintroducedaboveisthepreferredwaytochangethestatusbarstyleiniOS7.Alternatively,youcansetthestatusbarstylebyusingtheUIApplicationstatusBarStylemethod.Butfirstyou’llneedtooptoutthe“Viewcontroller-basedstatusbarappearance”.UndertheInfotaboftheprojecttarget,insertanewkeynamed“Viewcontroller-basedstatusbarappearance”andsetthevaluetoNO.Bydisablingthe“Viewcontroller-basedstatusbarappearance”,youcansetthestatusbarstylebyusingthefollowingcode:HidingtheStatusBarInanycaseyouwanttohidethestatusbar,youcanoverridetheprefersStatusBarHidden:inyourcontroller:SummaryiOS7presentsdeveloperswithnewfreedomtocustomizetheappearanceofnavigationbarandstatusbar.Ifyou’reportingtheappfromiOS6toiOS7orcreatingabrand-newappforiOS7,Ihopeyou’llfindthesetipsuseful.Foryourcompletereference,youcandownloadthesourcecodeofthedemoprojectfromhere.Justuncommentanycodesnippetsinthesampleprojecttotestoutthechange.Likemanyofyou,I’mstillexploringallthenewchangesofiOS7SDK.IambynomeansanexpertoniOS7.Ifyoufindanyerrorsinthearticle,pleasedoletmeknow.Ifyoufindanytipsandtricksrelatedtonavigationbarandstatusbar,pleasealsosharewithusbyleavingcommentbelow。还有一篇自定义iOS的NavigationBar,索性放在一起吧。Previously,inouriOSProgramming101series,weshowedyouhowtocustomizetheappearanceoftheTabBar.Inthistutorial,we’llcontinuetotalkaboutUIcustomizationandcoverhowtouseAppearanceAPItomaketheNavigationBarmorebeautiful.Herearewhatyou’lllearninthistutorial:CustomizingtheViewwithbackgroundp_w_picpathCustomizingUINavigationBarincludingbackgroundp_w_picpathandtextstyleoftitleCustomizingtheappearanceofUIBarButtonItemAsusual,we’regoingtoillustratetheconceptbyconvertingaplainnavigationbartoonewithcustomizedgraphics.However,tohelpyoufocusonlearningthecustomization,we’vepreparedtheXcodeprojectforyoutostartwith.Beforeproceeding,firstdownloadtheXcodeprojecthere(note:theprojectiscreatedusingXcode4.5).Ifyoubuildandruntheproject,you’llgetanappwithsimplenavigationUI.Nowwe’llworktogethertostylethenavigationbar,customizethebarbuttonsandassignourownbackgroundp_w_picpathfortheview.DemoApp–CustomNavigationBarCustomizingtheViewBackgroundFirst,wewouldliketochangethebackgroundofviewcontrollerwithourownp_w_picpath.IfyouopentheXcodeproject,you’llseeasetofp_w_picpathsthatwe’veaddedforyou.Tosetthebackgroundp_w_picpath,openthe“RecipeViewController.m”andaddthefollowinglineofcodetotheendof“viewDidLoad”method:Apparently,youcanusethe“backgroundColor”propertyoftheviewtochangethebackgroundcolor.Whatyoumaynotknowisthatyoucanalsousethesamepropertytosetthebackgroundp_w_picpath.ThetrickistocreateaUIColorobjectusingthe“colorWithPatternImage”.Duringdrawing,thep_w_picpathinthepatterncoloristiledasnecessarytocovertheviewarea.Afterthechange,compileandruntheapp.Itshouldlikethis:CustomizingtheBackgroundofViewAreaStylingtheUINavigationBarPriortoiOS5,developerscanonlychangethestyleofnavigationbarthroughahandfulofproperties.Buttheproblemisthechangeonlyappliestothenavigationbarofaspecificview.FromiOS5andonwards,theSDKallowsdeveloperstostylethenavigationbarbyusingAppearanceAPI.Youcaneasilycustomizetheappearanceofnavigationbarsthroughouttheappusingtheappearanceproxy([UINavigationBarappearance]).ChangingNavigationBarBackgroundOpen“AppDelegate.m”andaddthefollowinginthe“application:didFinishLaunchingWithOptions”method:ThefirstlineofcodecreatestheUIImageobjectwithourownbackgroundp_w_picpathofnavigationbar.Thenweusetheappearanceproxytoassignthep_w_picpath.CustomizingtheTitleTextofNavigationBarNext,we’llchangethefontstyleofthetitletext.Youcancustomizethetextstylebyusingthe“titleTextAttributes”propertiesofthenavigationbar.Youcanspecifythefont,textcolor,textshadowcolor,andtextshadowoffsetforthetitleinthetextattributesdictionary,usingthefollowingtextattributekeys:UITextAttributeFont–KeytothefontUITextAttributeTextColor–KeytothetextcolorUITextAttributeTextShadowColor–KeytothetextshadowcolorUITextAttributeTextShadowOffset–KeytotheoffsetusedforthetextshadowInthe“application:didFinishLaunchingWithOptions”methodofAppDelegate.m,addthefollowingcode:Theabovecodealtersthetextstyleofnavigationbartitle.WeusetheHelveticaNeue-CondensedBlackasthefonttype,setthecolortowhiteandaddashadowtothetex

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论