Ios 如何在本地存储UIImage以避免不必要的服务器请求?
我正在下载一个图像,然后用UIImageView显示它。使用这种方法,每次加载视图时都会下载图像。如何将其存储在本地以避免不必要的服务器请求Ios 如何在本地存储UIImage以避免不必要的服务器请求?,ios,objective-c,uiimageview,uiimage,Ios,Objective C,Uiimageview,Uiimage,我正在下载一个图像,然后用UIImageView显示它。使用这种方法,每次加载视图时都会下载图像。如何将其存储在本地以避免不必要的服务器请求 [self requestData ] ; UIImage *userImage = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:userProfileImageUrl ]]] ; [self.profileImageView set
[self requestData ] ;
UIImage *userImage = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:userProfileImageUrl ]]] ;
[self.profileImageView setImage:userImage ] ;
我建议使用这样的库来处理缓存和异步下载。第一次,您必须将图像保存到
NSDocumentsDirectory
中。为此,您必须采用directory的路径并像这样附加imageName
NSString * documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString * documentsPathAppend = [documentsPath stringByAppendingFormat:@"/%@",[userProfileImageUrl lastPathComponent]];
[self.profileImageView setImage:[UIImage imageWithContentsOfFile:imageEntity.imagePath]];
你必须用下列条件写出图像
if(![[NSFileManager defaultManager]fileExistsAtPath:documentsPathAppend])
{
NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:userProfileImageUrl]]];
[data writeToFile:documentsPathAppend atomically:YES];
}
之后,您必须将此路径添加到本地存储中,如核心数据。从下一次开始,您必须检查核心数据中特定URL的图像是否存在
NSString * documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString * documentsPathAppend = [documentsPath stringByAppendingFormat:@"/%@",[userProfileImageUrl lastPathComponent]];
[self.profileImageView setImage:[UIImage imageWithContentsOfFile:imageEntity.imagePath]];
尝试此方法将UIImage存储在本地目录中
- (void)addImage:(UIImage *)image toCacheWithIdentifier:(NSString *)identifier {
NSString *folderPath = @"LOCAL DIRECTORY PATH ";
if(![[NSFileManager defaultManager] fileExistsAtPath:folderPath isDirectory:nil])
{
[[NSFileManager defaultManager] createDirectoryAtPath:folderPath withIntermediateDirectories:YES attributes:nil error:nil];
}
NSString *fileName = [NSString stringWithFormat:@"%@.png",identifier];
fileName = [folderPath stringByAppendingPathComponent:fileName];
NSData *imageData = UIImagePNGRepresentation(image);
[imageData writeToFile:fileName atomically:YES];
}
要从本地目录检索图像,请尝试此方法
- (UIImage *)imageFromCacheWithIdentifier:(NSString *)identifier
{
NSString *folderPath = @"LOCAL DIRECTORY PATH ";
NSString *fileName = [NSString stringWithFormat:@"%@.png",identifier];
fileName = [folderPath stringByAppendingPathComponent:fileName];
if([UIImage imageWithContentsOfFile:fileName])
{
return [UIImage imageWithContentsOfFile:fileName];
}
return nil;
}
另一种可行的方法是在正式文档中使用
NSURLCache
(另请参见:)
HTTP缓存的发明正是为了满足这一需求:在RFC2616中
不幸的是,HTTP缓存是一个高级主题,
NSURLCache
的确切行为非常模糊。您只需将其保存到文档目录,并使用可以从URL派生的名称即可。然后在从URL加载之前,请尝试图像是否位于文档目录中。如果是这样,请从那里加载,而不是从URL加载。另一个选项是增加NSURLCache
的大小,使其存储图像的时间更长。请确保服务器在映像请求上设置了正确的过期日期。我认为您应该使用NSCache
将映像存储在缓存中。谢谢然后我会用URLWithString:DocumentsPapHappend代替userProfileImageUrl来设置这样的图像吗?:UIImage*userImage=[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:DocumentsPapHappend]]@hanumanDev,如果您只想下载映像并从documents目录中获取,那么请将具有[self.profileImageView setImage:[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:DocumentsPapHappend]]的其他文件放入其中;无需使用核心数据。如果您想下载图像并永久存储,请使用我的更新答案