文件GGImagenameKey.h、GGImagenameKey.m用于管理图片的名字。由一个键值,根据是iPhone、iPad、横屏、竖屏等关联出一个图片的名字,从而实现图片名字资源的统一管理。
文件GGImageManageService.h、GGImageManageService.m用于管理图片资源,现在只有加载图片资源的功能。
// 文件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 = [NSDictionarydictionaryWithObjectsAndKeys:
// 以此为iPhone横屏、iPhone竖屏、iPad横屏、iPad竖屏对应的图片名字
[NSArrayarrayWithObjects:@"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 = [superinit]) {
_datas = [GGImagenameKeyimagenameDatas];
[_datasretain];
NSInteger orIEntation = [[UIApplication sharedApplication] statusbarOrIEntation]; // 获取屏幕方向
if (orIEntation ==UIInterfaceOrIEntationLandscapeleft || orIEntation == UIInterfaceOrIEntationLandscapeRight) {
_index =0;
}
else {
_index =1;
}
if (0/**设备为iPad */) {
_index = _index +2;
}
}
returnself;
}
+ (voID)shutdownService {
SAFE_RELEASE(sGGImageManageService);
}
+ (voID)createSelf {
if (sGGImageManageService ==nil) {
sGGImageManageService = [[GGImageManageServicealloc] init];
}
}
+ (Nsstring *)imagenameForKey:(Nsstring *)key {
[GGImageMangeServicecreateSelf];
return [[sGGImageManageService.datasobjectForKey:key] objectAtIndex:GGImageMangeService.index];
}
+ (UIImage *)imageForKey:(Nsstring *)key {
Nsstring *path = [[NSBundlemainBundle] pathForResource:[GGImageMangeServiceimagenameForKey:key] ofType:nil];
return [UIImageimageWithContentsOffile:path];
}
+ (UIImage *)imageCacheForKey:(Nsstring *)key {
return [UIImageimagenamed:[GGImageMangeServiceimagenameForKey:key]];
}
- (voID)dealloc {
SAFE_RELEASE(_datas);
[superdealloc];
}
@end
// GGImageMangeService end
说明:应用程序退出时,需要调用[GGImageMangeService shutdownService];
总结以上是内存溢出为你收集整理的iPhone、iPad开发之图片资源管理全部内容,希望文章能够帮你解决iPhone、iPad开发之图片资源管理所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)