imageCache.h
//
// ImageCache.h
// nowLearn
//
// Created by wang doublejie on 12-9-12.
// Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//
//图片缓存类
#import <Foundation/Foundation.h>
@interface ImageCache : NSObject
{
}
+(UIImage*)loadImage:(NSString*)imageName;
+(void)releaseCache;
@end
imageCache.m
//
// ImageCache.m
// nowLearn
//
// Created by wang doublejie on 12-9-12.
// Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//
#import "ImageCache.h"
@implementation ImageCache
static NSMutableDictionary *dict;
+(UIImage*)loadImage:(NSString *)imageName
{
if(!dict)dict= [[NSMutableDictionary dictionary] retain];
UIImage* image= [dict objectForKey:imageName];
if (!image) {
NSString* imagePath = [[NSBundle mainBundle]pathForResource:imageName ofType:nil];
image = [UIImage imageWithContentsOfFile:imagePath];
if (image) {
[dict setObject:image forKey:imageName];
}
}
return image;
}
+(void)releaseCache
{
if (dict) {
[dict removeAllObjects];
}
}
@end
//使用
- (void)viewDidLoad
{
[super viewDidLoad];
NSMutableArray *images =[[NSMutableArray alloc]initWithCapacity:30];
for (int i=1; i<=9; i++) {
NSString* imageName=[NSString stringWithFormat:@"page_%d.jpg",i];
UIImage *image=[ImageCache loadImage:imageName];
[images addObject:image];
}
imageView=[[UIImageView alloc]initWithFrame:CGRectMake(0, 100, 320, 320)];
imageView.animationImages=images;
[images release];
imageView.animationDuration=10;
imageView.animationRepeatCount=100;
[imageView startAnimating];
[self.view addSubview:imageView];
UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
btn.frame = CGRectMake(30, 420, 90, 35);
[btn setTitle:@"清除内存占用" forState:UIControlStateNormal];
[btn setTitle:@"清除内存占用" forState:UIControlStateHighlighted];
[btn addTarget:self action:@selector(clearMemory:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn];
// Do any additional setup after loading the view.
}