Ios 如何在巨大的CollectionView中使用SDWebImage或AFNetworking在磁盘上缓存图像?
我正在我的应用程序中使用Ios 如何在巨大的CollectionView中使用SDWebImage或AFNetworking在磁盘上缓存图像?,ios,iphone,objective-c,ios7,afnetworking,Ios,Iphone,Objective C,Ios7,Afnetworking,我正在我的应用程序中使用AFNetworking下载图像的de Url、代码和详细信息。我将所有这些NSStrings存储在一个NSObject中,这些对象位于NSMutableArray中。我使用该NSMutableArray异步获取带有SDWebImage的图像,但在下载一些图像后,我的应用程序崩溃,出现收到内存警告,我尝试在AFNetworking中使用相同的方法,但什么都没有发生。我对这两个库都使用这种方法 [self.pictureImg setImageWithURL:[NSURL
AFNetworking
下载图像的de Url、代码和详细信息。我将所有这些NSStrings
存储在一个NSObject中,这些对象位于NSMutableArray
中。我使用该NSMutableArray
异步获取带有SDWebImage
的图像,但在下载一些图像后,我的应用程序崩溃,出现收到内存警告
,我尝试在AFNetworking
中使用相同的方法,但什么都没有发生。我对这两个库都使用这种方法
[self.pictureImg setImageWithURL:[NSURL URLWithString:imageUrl] placeholderImage:[UIImage imageNamed:kDefaultLoadingImage]];
我想知道如何缓存磁盘上的图像,以及如何在需要时清理缓存。我不在乎您是否使用AFNetworking
或SDWebImage
。我在自定义的单元格中使用该方法
谢谢大家! 只要使用这个库就可以了。它可以根据需要在磁盘上缓存图像。当您“收到内存警告”时调用此方法
[SDWebImageManager.sharedManager.imageCache clearMemory] 试试这个
SDImageCache*imageCache=[SDImageCache sharedImageCache]
[imageCache clearMemory] 对于这种方法,AFNetworking本身处理缓存,除非您不忽略NSURLRequest的cache属性试试这个!在AFT网络中为您的手机使用\uu-weak
[self.profileImage setImageWithURL:[NSURL URLWithString:urlString]
placeholderImage:[UIImage imageNamed:@"img_default"]
options:SDWebImageProgressiveDownload];
NSURLSessionConfiguration *sessionConfiguration = [NSURLSessionConfiguration defaultSessionConfiguration];
NSURLCache *cache = [[NSURLCache alloc] initWithMemoryCapacity:20 * 1024 * 1024 // 10MB. memory cache
diskCapacity:100 * 1024 * 1024 // 50MB. on disk cache
diskPath:nil];
sessionConfiguration.URLCache = cache;
sessionConfiguration.requestCachePolicy = NSURLRequestUseProtocolCachePolicy;
在collectionView中:cellForItemAtIndexPath:
__weak YOUR_CUSTOM_CELL *cell = (YOUR_CUSTOM_CELL *)[collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath];
NSURLRequest *requestL = [NSURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@", [your_model.url_image stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]]];
[your_cell.your_image_view setImageWithURLRequest:requestL placeholderImage:[UIImage imageNamed:@"test"] success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image) {
your_cell.your_image_view.image = image;
} failure:nil];
我希望这会有帮助