Iphone NSCache不起作用
我正在编写一个应用程序,需要在缓存中存储一些图像。我正在尝试使用NSCache,代码似乎很好,但不要将图像保存在缓存中。我有以下代码: 缓存是全局的,在.h:Iphone NSCache不起作用,iphone,objective-c,ios,xcode,nscache,Iphone,Objective C,Ios,Xcode,Nscache,我正在编写一个应用程序,需要在缓存中存储一些图像。我正在尝试使用NSCache,代码似乎很好,但不要将图像保存在缓存中。我有以下代码: 缓存是全局的,在.h:NSCache*缓存中声明 -(UIImage *)buscarEnCache:(UsersController *)auxiliarStruct{ UIImage *image; [[cache alloc] init]; NSLog(@"cache: %i", [cache countLimit]);
NSCache*缓存中声明代码>
-(UIImage *)buscarEnCache:(UsersController *)auxiliarStruct{
UIImage *image;
[[cache alloc] init];
NSLog(@"cache: %i", [cache countLimit]);
if ([cache countLimit] > 0) { //if [cache countLimit]>0, it means that cache isn't empty and this is executed
if ([cache objectForKey:auxiliarStruct.thumb]){
image = [cache objectForKey:auxiliarStruct.thumb];
}else{ //IF isnt't cached, is saved
NSString *imageURLString = [NSString stringWithFormat:@"http://mydomain.com/%@",auxiliarStruct.thumb];
NSURL *imageURL = [NSURL URLWithString:imageURLString];
NSData * imageData = [NSData dataWithContentsOfURL:imageURL];
image = [UIImage imageWithData:imageData];
[cache setObject:image forKey:auxiliarStruct.thumb];
}
}else{ //This if is executed when cache is empty. IS ALWAYS EXECUTED BECAUSE FIRST IF DOESN'T WORKS CORRECTLY
NSString *imageURLString = [NSString stringWithFormat:@"http://mydomain.com/%@",auxiliarStruct.thumb];
NSURL *imageURL = [NSURL URLWithString:imageURLString];
NSData * imageData = [NSData dataWithContentsOfURL:imageURL];
image = [UIImage imageWithData:imageData];
[cache setObject:image forKey:auxiliarStruct.thumb];
}
return image;
}
此函数在其他函数中通过以下方式调用:
UIImage *image = [self buscarEnCache:auxiliarStruct];
这是因为图像显示在屏幕上,但没有保存在缓存中,我认为失败的一行是:
[cache-setObject:image-forKey:auxiliarStruct.thumb]//auxiliarStruct.thumb是图像的名称
有人知道为什么缓存不工作??谢谢
ps:对不起,我的英语不好,我知道每次调用方法buscarEnCache:
时,都会创建一个新的缓存对象,行为:
[[cache alloc] init];
因此,旧缓存刚刚泄漏,不再可用
放置缓存=[[NSCache alloc]init]类的init方法中的code>
无需检查计数限制
-(UIImage *)buscarEnCache:(UsersController *)auxiliarStruct{
UIImage *image = [cache objectForKey:auxiliarStruct.thumb];
if (!image) {
NSString *imageURLString = [NSString stringWithFormat:@"http://mydomain.com/%@",auxiliarStruct.thumb];
NSURL *imageURL = [NSURL URLWithString:imageURLString];
NSData * imageData = [NSData dataWithContentsOfURL:imageURL];
image = [UIImage imageWithData:imageData];
[cache setObject:image forKey:auxiliarStruct.thumb];
}
return image;
}
您可能希望将图像的获取放在另一个线程中运行的方法中,并返回某种占位符图像。每次调用方法buscarEnCache:
时,都会使用以下行创建一个新的缓存对象:
[[cache alloc] init];
因此,旧缓存刚刚泄漏,不再可用
放置缓存=[[NSCache alloc]init]类的init方法中的code>
无需检查计数限制
-(UIImage *)buscarEnCache:(UsersController *)auxiliarStruct{
UIImage *image = [cache objectForKey:auxiliarStruct.thumb];
if (!image) {
NSString *imageURLString = [NSString stringWithFormat:@"http://mydomain.com/%@",auxiliarStruct.thumb];
NSURL *imageURL = [NSURL URLWithString:imageURLString];
NSData * imageData = [NSData dataWithContentsOfURL:imageURL];
image = [UIImage imageWithData:imageData];
[cache setObject:image forKey:auxiliarStruct.thumb];
}
return image;
}
您可能希望将图像的获取放在另一个线程中运行的方法中,并返回某种占位符图像。以及@rckoenes提供的答案,您没有正确分配缓存实例;应该是:
cache = [[NSCache alloc] init];
应该将其移动到您的init
方法中。以及@rckoenes提供的答案,您没有正确分配缓存实例;应该是:
cache = [[NSCache alloc] init];
应将其移动到您的init
方法中。我将该行移动到viewDidLoad,但结果相同。我将该行移动到viewDidLoad,但结果相同。很抱歉,我在回答中出错,理论还是一样的,只是你的alloc和init的方式不对。我把那一行移到viewDidLoad,但结果是一样的。很抱歉,我的答案出错了,理论还是一样的,只是你的alloc和init的方式不对。