用于存储UIImage ios的NSCache初始化 [英] NSCache initialization for storing UIImage ios

查看:111
本文介绍了用于存储UIImage ios的NSCache初始化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用NSCache存储图像。但问题是,一旦我在控制器之间切换,NSCache会清空。我希望项目至少存在,直到应用程序关闭或用户注销。
假设我有一个标签视图,我在第一个标签中存储数据中的图像。当我转到第二个选项卡并切换回第一个选项卡时,NSCache将再次初始化。

I am using NSCache to store images. But problem here is once I switch between controllers , NSCache empties. I want the items to be there atleast until application is closed or user logs out . Lets say I have a tab view and I am storing images from the data in 1st tab . When I go to second tab and switch back to first ,NSCache is initialized again.

这是我的代码: -

Here is my code :-

- (void)viewDidLoad {
[super viewDidLoad];
if(imageCache==nil)
{
    imageCache=[[NSCache alloc]init];
    NSLog(@"initialising");
}
[imageCache setEvictsObjectsWithDiscardedContent:NO];
}


(void) reloadMessages {

[Data getClassMessagesWithClassCode:_classObject.code successBlock:^(id object) {
    NSMutableArray *messagesArr = [[NSMutableArray alloc] init];
    for (PFObject *groupObject in object) {

        PFFile *file=[groupObject objectForKey:@"attachment"];
        NSString *url1=file.url;
        NSLog(@"%@ is url to the image",url1);
        UIImage *image = [imageCache objectForKey:url1];
        if(image)
        {
            NSLog(@"This is cached");

        }
        else{

            NSURL *imageURL = [NSURL URLWithString:url1];
            UIImage *image = [[UIImage alloc] initWithData:[NSData dataWithContentsOfURL:imageURL]];

            if(image)
            {
                NSLog(@"Caching ....");
                [imageCache setObject:image forKey:url1];
            }

        }

    }

控件永远不会转到第一个if语句。我错过了什么吗?

The control never goes to the first if statement. Am I missing something?

推荐答案

@interface Sample : NSObject

+ (Sample*)sharedInstance;

// set
- (void)cacheImage:(UIImage*)image forKey:(NSString*)key;
// get
- (UIImage*)getCachedImageForKey:(NSString*)key;

@end

#import "Sample.h"

static Sample *sharedInstance;

@interface Sample ()
@property (nonatomic, strong) NSCache *imageCache;
@end

@implementation Sample

+ (Sample*)sharedInstance {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        sharedInstance = [[Sample alloc] init];
    });
    return sharedInstance;
}
- (instancetype)init {
    self = [super init];
    if (self) {
        self.imageCache = [[NSCache alloc] init];
    }
    return self;
}

- (void)cacheImage:(UIImage*)image forKey:(NSString*)key {
    [self.imageCache setObject:image forKey:key];
}

- (UIImage*)getCachedImageForKey:(NSString*)key {
    return [self.imageCache objectForKey:key];
}

在您的代码中:

UIImage *image = [[Sample sharedInstance] getCachedImageForKey:url1];
    if(image)
    {
        NSLog(@"This is cached");

    }
    else{

        NSURL *imageURL = [NSURL URLWithString:url1];
        UIImage *image = [[UIImage alloc] initWithData:[NSData dataWithContentsOfURL:imageURL]];

        if(image)
        {
            NSLog(@"Caching ....");
            [[Sample sharedInstance] cacheImage:image forKey:url1];
        }

    }




  1. 如果App进入后台,NSCache将清除。

  1. If App goes in background NSCache will cleans.

您总是创建一个新缓存,更好的方法是将sharedInstance与一个NSCache对象一起使用。

You always create a new cache, better way is use sharedInstance with only one NSCache object.

这篇关于用于存储UIImage ios的NSCache初始化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆