2014-01-30 139 views
2

我需要設置圖像到我的imageViews。還有很多圖片(我認爲它將會接近200mb)。我需要保存全部,因爲在沒有互聯網連接的情況下使用本地應用這是非常容易使用類別UIImageView+AFNetworking,但我不明白它是如何保存和在哪裏?UIImageView + AFNetworking和保存圖像

因此,在訂閱方法here 時,您可以看到它使用的緩存策略爲NSURLCacheStorageAllowed。所以圖像保存在磁盤上的緩存文件夾中,對嗎?沒關係,但是這個存儲有什麼限制?我是否需要執行下面的代碼:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{ 
    //another code... 
    NSURLCache *URLCache = [[NSURLCache alloc] initWithMemoryCapacity:4 * 1024 * 1024 
                diskCapacity:200 * 1024 * 1024 
                 diskPath:nil]; 
    [NSURLCache setSharedURLCache:URLCache]; 
    return YES; 
} 

所以NSURLCacheStorageAllowedNSCachedURLResponse回到像storagePolicy。所以我明白我不能實現我上面寫的代碼。

如果我將使用UIImageView+AFNetworking類別,我的所有圖像是否會保存在緩存存儲器中?

回答

0

我已經做有點想,當我需要顯示的tableView圖像一樣,首先我檢查圖像還可以在當地或沒有,那麼我下載的圖片,並將其保存這樣

if (userBasicInfo.userImage == nil) { 
      __weak LGMessageBoxCell *weakCell = cell; 
      [cell.userImage setImageWithURLRequest:[[NSURLRequest alloc] initWithURL:[NSURL URLWithString:userBasicInfo.imageUrl]] 
            placeholderImage:[UIImage imageNamed:@"facebook-no-user.png"] 
              success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image){ 
               weakCell.userImage.image = image; 
               [weakCell setNeedsLayout]; 

               [MagicalRecord saveWithBlock:^(NSManagedObjectContext *localContext) { 
                UserBasicInfo* userBasicInfo = [[UserBasicInfo findByAttribute:@"userId" withValue:@(chatUser) inContext:localContext] objectAtIndex:0]; 
                userBasicInfo.userImage = UIImagePNGRepresentation(image); 
               } completion:^(BOOL success, NSError *error) { 
                NSLog(@"%@",[error localizedDescription]); 
               }]; 

              } 
              failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error){ 
              }]; 
     } else { 
      cell.userImage.image = [UIImage imageWithData:userBasicInfo.userImage]; 
     } 
3

的UIImageView +正如您發現的,AFNetworking依靠基礎URL加載系統將數據緩存到磁盤。 diskCapacity將決定您的應用一次需要多少存儲空間。這也將依賴於服務器指定適當的Cache-Control標題時處理圖像 - 在某些情況下,如果緩存時間太短,NSURLCache根本不會存儲它。

要更好地控制客戶端的磁盤緩存,您可以查看SDWebImage

SDWebImage具有異步圖像下載功能,對緩存有很多控制權 - 哪些圖像被緩存,在磁盤或內存中存儲多長時間等等。如果您需要保證圖像存儲一段特定時間, UIImageView + AFNetworking可能不會給你你需要的控制,你應該探索這個選擇。

相關問題