读书人

ios图片缓存播发

发布时间: 2012-09-29 10:30:01 作者: rapoo

ios图片缓存播放

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= [[NSMutableDictionarydictionary] retain];

UIImage* image= [dictobjectForKey:imageName];

if (!image) {

NSString* imagePath = [[NSBundlemainBundle]pathForResource:imageNameofType:nil];

image = [UIImage imageWithContentsOfFile:imagePath];

if (image) {

[dict setObject:imageforKey:imageName];

}

}

return image;

}

+(void)releaseCache

{

if (dict) {

[dict removeAllObjects];

}

}


@end



//使用

- (void)viewDidLoad

{

[superviewDidLoad];

NSMutableArray *images =[[NSMutableArrayalloc]initWithCapacity:30];

for (int i=1; i<=9; i++) {

NSString* imageName=[NSStringstringWithFormat:@"page_%d.jpg",i];

UIImage *image=[ImageCacheloadImage:imageName];

[images addObject:image];

}

imageView=[[UIImageViewalloc]initWithFrame:CGRectMake(0, 100, 320, 320)];

imageView.animationImages=images;

[images release];

imageView.animationDuration=10;

imageView.animationRepeatCount=100;

[imageView startAnimating];

[self.viewaddSubview:imageView];


UIButton *btn = [UIButtonbuttonWithType:UIButtonTypeRoundedRect];

btn.frame = CGRectMake(30, 420, 90, 35);

[btn setTitle:@"清除内存占用" forState:UIControlStateNormal];

[btn setTitle:@"清除内存占用" forState:UIControlStateHighlighted];

[btn addTarget:selfaction:@selector(clearMemory:)forControlEvents:UIControlEventTouchUpInside];

[self.viewaddSubview:btn];

// Do any additional setup after loading the view.

}





读书人网 >操作系统

热点推荐