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= [[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.

}





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值