2014-10-06 52 views
1

當第一次加載圖像(通過URL)時,圖像加載應該是動畫的。之後,滾動tableview不應該觸發動畫。UITableViewCell AFNetworking只加載一次動畫的圖像

下面的代碼有問題:只要UITableViewCell出列,就會觸發動畫,導致圖像從出列單元中消失。如何讓動畫只觸發一次?

[thumbnail cancelImageRequestOperation]; 
NSURL* imageUrl = [NSURL URLWithString:imageUrlString]; 
if (imageUrl) { 
    [thumbnail setImageWithURLRequest:[NSURLRequest requestWithURL:imageUrl] 
        placeholderImage:placeholderImage 
           success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image) { 
             [UIView transitionWithView:thumbnail 
                 duration:0.5f 
                 options:UIViewAnimationOptionTransitionCrossDissolve 
                 animations:^{thumbnail.image = image;} 
                 completion:nil]; 

           } 
           failure:nil]; 
} 

回答

0

我認爲你應該將圖像存儲爲數據源的屬性,或者應該將圖像存儲在磁盤上。

在完成請求後,您必須將圖像存儲爲屬性。

if(!obj.image) 
NSURL* imageUrl = [NSURL URLWithString:imageUrlString]; 
if (imageUrl) { 
[thumbnail setImageWithURLRequest:[NSURLRequest requestWithURL:imageUrl] 
       placeholderImage:placeholderImage 
          success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image) { 
            [UIView transitionWithView:thumbnail 
                duration:0.5f 
                 options:UIViewAnimationOptionTransitionCrossDissolve 
                animations:^{thumbnail.image = image;} 
                completion:{ 
obj.image = image; 
cell.image = obj.image; 
}]; 

          } 
          failure:nil]; 
} 
} 
else 
{ 
    cell.image = obj.image; 
} 

對於保存到磁盤你必須做同樣只需要檢查如果圖像是存在於磁盤,然後從磁盤設置或者調用下載。

0

問題是,你永遠不會檢查圖像是否已經爲該特定行下載。

每次下載你的圖片,你需要在一些地方保存(類似NSMutableArray)在success塊。在致電setImageWithURLRequest之前,請檢查您的陣列以查看該行的圖像是否已存在。如果是這樣,請在本地加載。如果不是,則只有,然後請致電setImageWithURLRequest

0

如果你看一下UIImageView+AFNetworking.h,它明確指出,在由-setImageWithURLRequest:placeholderImage:success:failure:叫成功塊:

如果圖像是從緩存返回,請求和響應 參數是nil

只有當請求和響應參數非零時,才能使用此事實來運行動畫。

另外,-setImageWithURLRequest:placeholderImage:success:failure:實際上的形象。我不確定您需要在動畫塊中再次執行操作。

+0

謝謝。該描述還說:如果指定了成功塊,則在返回之前設置圖像視圖的圖像是該塊的責任。如果未指定成功塊,則應用使用'self.image = image'設置圖像的默認行爲。 – Raymond 2014-10-09 03:27:24