2017-07-07 53 views
2

我有一個加載5個圖像到TableView的每個單元的問題。我用AFNetworking下載和setImageWithUrl方法imageView異步加載多個圖像到單個單元

for (int i = 0; i < urls.count; i++) { 
    [self.photoImageView[i] setImageWithURL:urls[i]]; 
} 

TableView非常剎車,當我把它的滾動。但是當我嘗試加載1或2時滾動沒有剎車。 enter image description here 如何更正沒有剎車的下載圖像?

+0

image without break? –

+0

嘗試設置'tableView.separatorStyle = UITableViewCellSeparatorStyleNone;' –

+0

它打破原因圖像尺寸更大,所以儘量減少它。 –

回答

2

我認爲這對我的代碼工作有幫助。

首先你使用這個庫SDWebImage 然後執行下面的代碼。

#import <SDWebImage/UIImageView+WebCache.h> 
... 
[cell.imageView sd_setImageWithURL:[NSURL URLWithString:@"URL"] 
      placeholderImage:[UIImage imageNamed:@"placeholder.png"]]; 
1

您可以使用@Ravi討論的SDWebImage。您可以在for循環中使用上面的代碼來異步顯示所有5個圖像,而不會阻塞主線程。

for (int i = 0; i < urls.count; i++) { 
[self.photoImageView[i] sd_setImageWithURL:[NSURL URLWithString:urls[i]] 
     placeholderImage:[UIImage imageNamed:@"placeholder.png"]]; 
} 

如果你不想使用任何第三方庫,你也可以使用GCD相同。檢查這ans

希望它有幫助。快樂編碼!

0

如果你看到UIImage + AFNetworking類,它內部加載圖像使用異步NSOperations.Try加載圖像的特定dimensions.It將下載圖像的大小&可以提高滾動性能。

NSMutableString *newURLPath = [NSMutableString stringWithString[exercise.imageURL absoluteString]]; 
[newURLPath appendFormat:@"?width=200&"]; 
[newURLPath appendFormat:@"height=200"]; 
1

For downloading the Asynchronous image from url, use any of these library for improving the scroll performance

SDWebImage - 異步圖像下載使用緩存的支持作爲一個UIImageView類。

#import <SDWebImage/UIImageView+WebCache.h> 
... 
[imageView sd_setImageWithURL:URL 
      placeholderImage:[UIImage imageNamed:@"placeholder.png"] 
]; 

PINRemoteImage - 它下載一個漸進式JPEG並獲得吸引人的模糊更新。

對於你的情況,你可以去SDWebImage。

For Advance Concept, you could look into this one. 

AsyncDisplayKit - 適用於iOS光滑異步用戶界面的應用程式

即使最近,Pinterest的是建立在AsyncDisplayKit以保持用戶界面平滑和響應的頂部。 閱讀這篇文章的更多信息:https://medium.com/@Pinterest_Engineering/re-architecting-pinterest-039-s-ios-app-e0a2d34a6ac2