缓存URL图片iphone UITableview

dub*_*eat 11 iphone cocoa-touch uitableview image-caching ios

我正在寻找一个关于如何将从url加载的图像缓存到uitableview的单元格中的教程.

我在这里找到了一个例子

http://www.ericd.net/2009/05/iphone-caching-images-in-memory.html#top

但代码不完整.我是一个客观的新手所以我发现填补缺失的部分非常困难.

Fle*_*lea 35

这是一个使用NSCache的简单ImageCache实现.ImageCache是​​一个singelton.

ImageCache.h

    #import <Foundation/Foundation.h>

    @interface ImageCache : NSObject

    @property (nonatomic, retain) NSCache *imgCache;


    #pragma mark - Methods

    + (ImageCache*)sharedImageCache;
    //- (void) AddImage:(NSString *)imageURL: (UIImage *)image;
   - (void) AddImage:(NSString *)imageURL withImage:(UIImage *)image;
    - (UIImage*) GetImage:(NSString *)imageURL;
    - (BOOL) DoesExist:(NSString *)imageURL;

    @end
Run Code Online (Sandbox Code Playgroud)

ImageCache.m

  #import "ImageCache.h"

    @implementation ImageCache

    @synthesize imgCache;

    #pragma mark - Methods

    static ImageCache* sharedImageCache = nil;

    +(ImageCache*)sharedImageCache
    {
        @synchronized([ImageCache class])
        {
            if (!sharedImageCache)
                sharedImageCache= [[self alloc] init];

            return sharedImageCache;
        }

        return nil;
    }

    +(id)alloc
    {
        @synchronized([ImageCache class])
        {
            NSAssert(sharedImageCache == nil, @"Attempted to allocate a second instance of a singleton.");
            sharedImageCache = [super alloc];

            return sharedImageCache;
        }

        return nil;
    }

    -(id)init 
    {
        self = [super init];
        if (self != nil) 
        {
            imgCache = [[NSCache alloc] init];
        }

        return self;
    }

   // - (void) AddImage:(NSString *)imageURL: (UIImage *)image
- (void) AddImage:(NSString *)imageURL withImage:(UIImage *)image
    {
        [imgCache setObject:image forKey:imageURL];
    }

    - (NSString*) GetImage:(NSString *)imageURL
    {
        return [imgCache objectForKey:imageURL];
    }

    - (BOOL) DoesExist:(NSString *)imageURL
    {
        if ([imgCache objectForKey:imageURL] == nil)
        {
            return false;
        }

        return true;
    }


    @end
Run Code Online (Sandbox Code Playgroud)

UIImage *image;

    // 1. Check the image cache to see if the image already exists. If so, then use it. If not, then download it.

    if ([[ImageCache sharedImageCache] DoesExist:imgUrl] == true)
    {
        image = [[ImageCache sharedImageCache] GetImage:imgUrl];
    }
    else
    {
        NSData *imageData = [[NSData alloc] initWithContentsOfURL: [NSURL URLWithString: imgUrl]];
        image = [[UIImage alloc] initWithData:imageData];

        // Add the image to the cache 
        //[[ImageCache sharedImageCache] AddImage:imgUrl :image];

        [[ImageCache sharedImageCache] AddImage:imgUrl withImage:image];
    }
Run Code Online (Sandbox Code Playgroud)

  • 这个答案对我有帮助 (2认同)

dub*_*eat 8

这里找到了一个很好的工作实例

http://ezekiel.vancouver.wsu.edu/~wayne/yellowjacket/YellowJacket.zip

  • iOS 5的更新版本:`git clone http:// ezekiel.vancouver.wsu.edu /~cs458/demos/YellowJackets/.git` [link](http://ezekiel.vancouver.wsu.edu/~cs458/演示/ ImageCache/HTML /图像缓存/) (4认同)