2014-02-06 198 views
0

我有一个代码来显示来自远程URL的图像。如何显示UIImageView的加载图像

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ 
     NSData *imageData = nil; 

     imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:image]]; 

     if (imageData){ 
      dispatch_async(dispatch_get_main_queue(), ^{ 
       // UIKit, which includes UIImage warns about not being thread safe 
       // So we switch to main thread to instantiate image 
       UIImage *image1 = [UIImage imageWithData:imageData]; 
       self.imageLabel.image = image1; 


      }); 
     } 
    }); 

但下载图像需要2秒。我试图把动画加载图像到UIImageView,但没有奏效。我如何在2秒内实现加载图像?

回答

2

通常iOS中没有提供占位符图像的内置方法。您可以使用名为SDWebImage.framework的框架完成此任务。在我的一个项目中,我必须显示占位符图像,同时从服务器加载主图像。我使用了相同的框架并使用UICollectionView显示图像,我使用的代码是:

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView 
      cellForItemAtIndexPath:(NSIndexPath *)indexPath 
{ 
    CollectionCell *Cell = [collectionView 
          dequeueReusableCellWithReuseIdentifier:@"MyCell" 
          forIndexPath:indexPath]; 



    [Cell.imageview setImageWithURL:url placeholderImage:[UIImage imageNamed:@"image.png"]]; 

    return myCell; 
} 
相关问题