我已经使用 Parse.com 构建了一个 iOS 应用程序。
在我的应用程序中,我显示了来自网络的大量数据。
为了快速加载数据,我使用了缓存kPFCachePolicyCacheThenNetwork。它可以很好地加载数据,但是在滚动图像时需要一些时间来加载。
首先是白色图像,然后是图像加载。
Is there any solution like cache to display images in imageviews with out interruption.
谢谢。
我已经使用 Parse.com 构建了一个 iOS 应用程序。
在我的应用程序中,我显示了来自网络的大量数据。
为了快速加载数据,我使用了缓存kPFCachePolicyCacheThenNetwork。它可以很好地加载数据,但是在滚动图像时需要一些时间来加载。
首先是白色图像,然后是图像加载。
Is there any solution like cache to display images in imageviews with out interruption.
谢谢。
您可以使用SDWebImage来执行此操作。
您也可以使用NSCache进行缓存。
是的,您可以使用AFNetworking UIImageview 类别并且您已排序
如果您编辑现有图像然后从相同的 URL 下载它,您还可以禁用缓存通常需要的特定图像,方法是操作其
-(void)setImageWithURL:(NSURL *)url
placeholderImage:(UIImage *)placeholderImage
如本博客所述
另一个选项可能是具有刷新缓存功能的ASImageView 。
我在 gcd 的帮助下编写代码,你只需要传递 imageView 的对象并传递 url ,它管理所有的东西,比如缓存。
-(void)downloadingServerImageFromUrl:(UIImageView*)imgView AndUrl:(NSString*)strUrl{
strUrl = [strUrl encodeUrl];
NSString* theFileName = [NSString stringWithFormat:@"%@.png",[[strUrl lastPathComponent] stringByDeletingPathExtension]];
NSFileManager *fileManager =[NSFileManager defaultManager];
NSString *fileName = [NSHomeDirectory() stringByAppendingPathComponent:[NSString stringWithFormat:@"tmp/%@",theFileName]];
imgView.backgroundColor = [UIColor darkGrayColor];
UIActivityIndicatorView *actView = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
[imgView addSubview:actView];
[actView startAnimating];
CGSize boundsSize = imgView.bounds.size;
CGRect frameToCenter = actView.frame;
// center horizontally
if (frameToCenter.size.width < boundsSize.width)
frameToCenter.origin.x = (boundsSize.width - frameToCenter.size.width) / 2;
else
frameToCenter.origin.x = 0;
// center vertically
if (frameToCenter.size.height < boundsSize.height)
frameToCenter.origin.y = (boundsSize.height - frameToCenter.size.height) / 2;
else
frameToCenter.origin.y = 0;
actView.frame = frameToCenter;
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_async(queue, ^{
NSData *dataFromFile = nil;
NSData *dataFromUrl = nil;
dataFromFile = [fileManager contentsAtPath:fileName];
if(dataFromFile==nil){
dataFromUrl=[[[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:strUrl]] autorelease];
}
dispatch_sync(dispatch_get_main_queue(), ^{
if(dataFromFile!=nil){
imgView.image = [UIImage imageWithData:dataFromFile];
}else if(dataFromUrl!=nil){
imgView.image = [UIImage imageWithData:dataFromUrl];
NSString *fileName = [NSHomeDirectory() stringByAppendingPathComponent:[NSString stringWithFormat:@"tmp/%@",theFileName]];
BOOL filecreationSuccess = [fileManager createFileAtPath:fileName contents:dataFromUrl attributes:nil];
if(filecreationSuccess == NO){
NSLog(@"Failed to create the html file");
}
}else{
imgView.image = [UIImage imageNamed:@"NO_Image.png"];
}
[actView removeFromSuperview];
[actView release];
[imgView setBackgroundColor:[UIColor clearColor]];
});
});
}
这是在后台加载图像的代码。检查此代码
您应该使用 EGOImageLoading( EGOImageLoading ) 来延迟加载图像,并且可以使用 NSUserDefaults 保存图像。
这是我建议的最佳方式,因为我在许多项目中都使用过它。
很抱歉,Parse 已经在做所有这些事情了。Parse 使用 AFNetworking 来获取图像,将它们放在 NSCache 中,并非常积极地使用磁盘缓存,以便完全避免冗余的网络行程(版本是 URL 的一部分,因此 Parse 甚至不需要使用 if-modified -从 GET 开始)。
您可能遇到的最大问题是 PFFile 会限制下载,一次只能下载 3 个图像。经过实验,这是最好的数字,因为它确保了对用户的增量反馈,并避免了一些 iOS 设备在线程过多后性能非常差的讨厌问题。如果可能的话,我能给你的最好的建议是预取一些图像。如前所述,PFFile 图像被非常积极地缓存。kPFCachePolicy... 东西只会影响可能会变得陈旧的查询;这些查询指向的图像始终被缓存(尽管最初只是按需下载)。