[英]How to cache images on disk with SDWebImage or AFNetworking in a huge CollectionView?
我在我的應用程序中使用AFNetworking
下載圖像的網址,代碼和詳細信息。 我將所有這些NSStrings
存儲在NSMutableArray
中的那些對象的NSObject中。 我使用該NSMutableArray
異步地使用SDWebImage
獲取圖像,但是下載了某些圖像后,我的應用程序崩潰,並顯示Received memory warning
我嘗試在AFNetworking
使用相同的方法, AFNetworking
發生任何事情。 我在兩個庫中都使用了這種方法。
[self.pictureImg setImageWithURL:[NSURL URLWithString:imageUrl] placeholderImage:[UIImage imageNamed:kDefaultLoadingImage]];
我想知道如何在磁盤上cache
圖像,以及在需要時如何清理緩存。 我不在乎您是否使用AFNetworking
或SDWebImage
。 我在自定義cells
使用了該方法。
謝謝!
只需使用此FastImageChache庫。 它能夠根據需要在磁盤上緩存圖像。
當您“收到內存警告”時調用此方法
[SDWebImageManager.sharedManager.imageCache clearMemory];
嘗試這個,
SDImageCache * imageCache = [SDImageCache sharedImageCache];
[imageCache clearMemory];
對於此方法,AFNetworking本身會處理高速緩存,除非您不忽略NSURLRequest的高速緩存屬性
[self.profileImage setImageWithURL:[NSURL URLWithString:urlString]
placeholderImage:[UIImage imageNamed:@"img_default"]
options:SDWebImageProgressiveDownload];
嘗試這個! 在AFTNetworking中為您的單元格使用__weak
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];
希望對您有所幫助!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.