2015-04-02 63 views
0

我有一個基於Xcode objective-c的應用程序,它有一個使用PHP從MySql加載圖像和一些信息的tableview。除了圖像以外,一切都很好,當我向下滾動表格需要很長時間時,它會將圖像(圖像A在圖像B中)混合幾秒鐘,然後變爲其位置,特別是當我快速滾動表格時:在UITableView中加載時混合的圖像

她是我的代碼:

- (void)downloadImageWithURL:(NSURL *)url completionBlock:(void (^)(BOOL succeeded, UIImage *image))completionBlock 

{ 

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url]; 
[NSURLConnection sendAsynchronousRequest:request 
queue:[NSOperationQueue mainQueue] 
completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) { 
if (!error) 
{ 
UIImage *image = [[UIImage alloc] initWithData:data]; 
completionBlock(YES,image); 
} else{ 
completionBlock(NO,nil); 
} 
}]; 

} 


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 

{ 

NSString *one = listObjects.theUserId; // the listObjects where is store my info and the images Url 
    NSURL *myUrl = [NSURL URLWithString:one]; 


if (listObjects.image) { 
cell.myImageView.image = listObjects.image; 

} else { 

// set default user image while image is being downloaded 
cell.myImageView.image = [UIImage imageNamed:@"defaultImage.png"]; 

// download the image asynchronously 
[self downloadImageWithURL:myUrl completionBlock:^(BOOL succeeded, UIImage *image) { 
if (succeeded) { 

// change the image in the cell 
cell.myImageView.image = image; 
// cache the image for use later (when scrolling up) 
listObjects.image = image; 
} 

}]; 

} 


return cell; 


} 

請檢查我的代碼,我需要補充? 謝謝

+0

我有類似的問題。如果有任何問題,我會及時通知你。順便說一句我已經嘗試使用 - (void)prepareForReuse並且仍然沒有改變。 – 2015-04-06 21:13:42

回答

0

如果它適合你,我會加載tableview之前加載數組中的所有圖像。

在我看來,talbeview僅用於顯示數據,而不是讀取數據(這裏是做什麼)。

不是創建一個NSString對象的數組,而是創建自定義對象類並給它一個不錯的名字和一些你需要的屬性。例如:

Student是我的課,從NSObject的

繼承

其屬性

@property (weak, nonatomic) NSString *name; 
@property (weak, nonatomic) NSString *school; 
@property (weak, nonatomic) int *age; 
@property (weak, nonatomic) UIImage *picture; 

當它這樣做,創建加載所有你在網上所需要的數據的方法和創建自定義的物品運它。我正在跳過所有的下載部分,因爲你可能知道這一點。如果你不這樣做,那麼使用AFNetworking確實是一個好主意,正如Georgio提出的另一個答案中的建議。

我沒有很長的時間做這一點,但它是非常普遍和容易的,這將是一些簡單的像UIImage *image = [UIImage imageWithURL:YOURURL];或imageWithData:和你的數據來自一些你從一個URL獲得...

無論如何,你可以像現在一樣獲得你的圖像數據,只是在別的地方!然後,用你在線提取的任何東西創建你的Student對象,然後用這些對象的數組填充你的tableview。一旦你得到的一切好的和整潔,叫

[tableview reloadData]; 

,並在您cellForRow

Student *student = [myArrayOfFetchedData objectAtIndex:indexPath.row]; 
cell.image  = student.image; 
cell.title  = student.name; 
cell.subtitle = student.school; 
return cell; 

你可以/應該完全使用自定義單元格爲你的具體數據更appopriate顯示。這是另一個問題和另一個教程。

通過這樣做,你將確保:

  • 您的所有照片之前,實現代碼如下加載
  • 將不會有噱頭/滯後/毛刺,因爲我們總是以相同的處理加載OBJETS。

在cellforrow中執行異步工作只是要求麻煩並且會破壞用戶的體驗。

+0

謝謝Zil,我不夠好用你的方式。如果它正在工作,我會嘗試其他答案。否則,我會學習你的方式。 – 2015-04-07 16:15:50

+0

即使是初學者,我自己也很容易。最重要的是,這是一個很好的實踐,並且明確地說是一個很好的習慣;) 我非常強烈地建議你嘗試一下,即使它需要10倍的時間(如果你從來沒有做過,那應該是2個小時,永遠),你會得到更多的它 – 2015-04-07 16:17:30

+0

謝謝,我會認真對待它。我要去學習它 – 2015-04-07 16:26:08

0

有很多解決方案來解決這個問題。最好的是用AFNetworking加載圖像。您可以在這裏找到github項目:https://github.com/AFNetworking/AFNetworking
只需將「AFNetworking」文件夾複製到您的項目中,然後從「UIKit + AFNetworking」文件夾複製「ImageView + AFNetworking.h」和「ImageView + AFNetworking.m」(您也可以使用與UIKit中的許多其他元素AFNetworking」。
然後導入 「的ImageView + AFNetworking.h」 你的課

#import "UIImageView+AFNetworking.h" 

並加載圖像您- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath方法,使用補充一點:

[cell.myImageView setImageWithURL:myURL placeholderImage:nil]; 
+0

謝謝,我一到家就會嘗試。 – 2015-04-07 16:14:24