2017-05-08 50 views
1

我正在使用SDWebImage庫在頁面控件的滾動視圖中顯示圖像。從URL和緩存中逐一加載和顯示圖像

如何逐個下載圖片並顯示圖片。假設我有4個URL:url1,url2,url3,url4 所以我想先顯示url1然後... 2,3,4..so ... 2,3,4應該在後臺加載,直到沒有在那裏下載一個佔位符圖片展示。

下面是我的代碼裏面for循環我試試它是完美的嗎?

 NSString* serverImgicon=<url here in a loop>; 
dispatch_sync(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0),^{ 
      NSURL *url=[NSURL URLWithString: serverImgicon]; 
      //set your image on main thread. 
      dispatch_async(dispatch_get_main_queue(),^{ 
       [img sd_setImageWithURL:url 
         placeholderImage:[UIImage imageNamed:@"placeholderImg"]]; 
      }); 
     }); 
+1

按照這個答案[此處輸入鏈接的描述(http://stackoverflow.com/questions/18342172/load-images-using-sdwebimage-in-order) –

+0

如果圖像應該加載在背景然後什麼是加載一個接一個的目的.. – user2931321

回答

-1

使用sdwebCache裝入圖像異步

https://github.com/rs/SDWebImage

/******************/

#import "UIImageView+WebCache.h" 

然後像這樣加載圖片:

NSArray *tempArray = [stickersThumbRecords objectForKey:collectioName]; 

       if (tempArray.count>indexPath.item) { 
        NSURL *url = [NSURL URLWithString:[tempArray objectAtIndex:indexPath.item] ]; 

        [imageView setImageWithURL:url placeholderImage:[UIImage imageNamed:@"placeholderFrame"]]; 
       } 
+1

ajjjjjj我做同樣的事情...你檢查...我的問題是加載和顯示圖像的順序爲1乘1。 .. – iphonemaclover

+0

你面臨什麼問題? – ajjjjjjjj

+0

爲低連接像2g ...其下載同步圖像隨機圖像從URL ...意味着URL1是空的,URL2有圖像等... – iphonemaclover

1

嘗試下面的代碼,它會正常工作

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{ 

     CollectionViewCell *cell =[collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath]; 

     myobject *newobject = _myarray[indexPath.item]; 
     cell.nameLabel.text =newobject.name; 

      dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul); 
     dispatch_async(queue, ^{ 

      [cell.activityIndicatorForImage startAnimating]; 
      UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL: 
                [NSURL URLWithString:newobject.image]]]; 
      dispatch_sync(dispatch_get_main_queue(), ^{ 
       if(image!= nil){ 
        [[cell myimage] setImage:image]; 
        [cell.activityIndicatorForImage setHidden:NO]; 

       }else{ 
        UIImage * image = [UIImage imageNamed:@"PLACEHOLDER"]; 
        [[cell myimage] setImage:image]; 
        [cell.activityIndicatorForImage setHidden:YES]; 
        [cell.activityIndicatorForImage stopAnimating]; 

       } 


       [cell setNeedsLayout]; 
      }); 
     }); 

     return cell; 
    } 
+0

感謝sathish,但我需要使用SDWebImage代碼 – iphonemaclover