iPhone、iPad开发之图片资源管理
fmms
13年前
<p> 移动开发设计的设备、屏幕很多(如cocoa开发分为iPhone版、iPad版, 它们又分为横屏、竖屏),所以图片资源的统一管理是必要的。下面是自己想的一个解决方案,欢迎交流。</p> <p> 文件GGImageNameKey.h、GGImageNameKey.m用于管理图片的名字。由一个键值,根据是iPhone、iPad、横屏、竖屏等关联出一个图片的名字,从而实现图片名字资源的统一管理。</p> <p> 文件GGImageManageService.h、GGImageManageService.m用于管理图片资源,现在只有加载图片资源的功能。</p> <p></p> <pre class="brush:cpp; toolbar: true; auto-links: false;"> // 文件GGImageNameKey.h begin #import <Foundation/Foundation.h> @interface UZImageNameKey : NSObject { } + (NSDictionary *)imageNameDatas; @end // 这里定义图片资源的键值,有键值会关联出一个图片名 #define GGNextStepImage @"GGNextStepImage" ........... ............. // 文件GGImageNameKey.h end // 文件GGImageNameKey. begin #import "GGImageNameKey.h" @implementation GGImageNameKey + (NSDictionary *)imageNameDatas { /* 因为这个对象创建后放在自动释放池里,在下一次runLoop中会被销毁。 所以建议在程序运行中,该方法只调用一次,而且获取资源后,需要retain一份 */ NSDictionary *dict = nil; dict = [NSDictionary dictionaryWithObjectsAndKeys: // 以此为iPhone横屏、iPhone竖屏、iPad横屏、iPad竖屏对应的图片名字 [NSArray arrayWithObjects:@"GGNextStepIphoneL.png",@"GGNextStepIphoneP.png", @"GGNextStepIpadL.png",@"GGNextStepIpadP.png", nil], GGNextStepImage, ... ... nil]; return dict; } @end // GGImageNameKey.m end // GGImageManageService.h begin #import <Foundation/Foundation.h> #import "GGImageNameKey.h" // 获取图片名字 #define GGImageNameForKey(key) [GGImageManageService imageNameForKey:key] // 加载图片 #define GGImageCacheForKey(key) [GGImageManageService imageCacheForKey:key] #define GGImageForKey(key) [GGImageManageService imageForKey:key] @interface GGImageManageService : NSObject { NSDictionary *_datas; // 图片资源数据 NSInteger _index; // 需要获取的图片类型的索引 } // 由键值获取一张图片名 + (NSString *)imageNameForKey:(NSString *)key; // 加载一张图片 注意:这种图片加载方式,图片是加载到iPhone系统缓存 + (UIImage *)imageCacheForKey:(NSString *)key; // 加载一张图片 + (UIImage *)imageForKey:(NSString *)key; // 关闭服务 + (void)shutdownService; @property (nonatomic,retain) NSDictionary *datas; @property (nonatomic) NSInteger index; @end // GGImageManageService.m end // GGImageManageService.m begin #import "GGImageManageService.h" #import "GGImageNameKey.h" static GGImageManageService *sGGImageManageService =nil; @implementation UZImageManageService @synthesize datas = _datas; @synthesize index = _index; - (id)init { if (self = [super init]) { _datas = [GGImageNameKey imageNameDatas]; [_datasretain]; NSInteger orientation = [[UIApplication sharedApplication] statusBarOrientation]; // 获取屏幕方向 if (orientation ==UIInterfaceOrientationLandscapeLeft || orientation == UIInterfaceOrientationLandscapeRight) { _index =0; } else { _index =1; } if (0/**设备为iPad */) { _index = _index +2; } } return self; } + (void)shutdownService { SAFE_RELEASE(sGGImageManageService); } + (void)createSelf { if (sGGImageManageService ==nil) { sGGImageManageService = [[GGImageManageService alloc] init]; } } + (NSString *)imageNameForKey:(NSString *)key { [GGImageMangeService createSelf]; return [[sGGImageManageService.datasobjectForKey:key] objectAtIndex:GGImageMangeService.index]; } + (UIImage *)imageForKey:(NSString *)key { NSString *path = [[NSBundle mainBundle] pathForResource:[GGImageMangeService imageNameForKey:key] ofType:nil]; return [UIImage imageWithContentsOfFile:path]; } + (UIImage *)imageCacheForKey:(NSString *)key { return [UIImage imageNamed:[GGImageMangeService imageNameForKey:key]]; } - (void)dealloc { SAFE_RELEASE(_datas); [super dealloc]; } @end // GGImageMangeService end</pre> <p class="p7"></p> <p class="p7"> 说明:应用程序退出时,需要调用[GGImageMangeService shutdownService];</p>