2016-11-27 14 views
0

写真を表示するコレクションビューがあります。UIImageViewは、ダウンロードした画像をUICollectionViewCellに表示しません。

Assets.xcassetsから一時イメージを表示するとすべて動作しますが、インターネットからイメージをダウンロードすると何も表示されません。

CollectionViewController.swift

class CollectionViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout { 
    @IBOutlet var collectionView: UICollectionView! 

    var currentCategory:PhotoCategory! 

    func numberOfSections(in collectionView: UICollectionView) -> Int { 
     return 1 
    } 

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 
     return currentCategory.images.count 
    } 

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 
     let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "GalleryItemCollectionViewCell", for: indexPath) as! GalleryItemCollectionViewCell 

     cell.setGalleryItem(imageURL: currentCategory.images[indexPath.row].image) 
     return cell 
    } 

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize { 
     let picDimension = self.view.frame.size.width/4.0 
     return CGSize(width: picDimension, height: picDimension) 
    } 

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets { 
     let leftRightInset = self.view.frame.size.width/20.0 
     return UIEdgeInsetsMake(0, leftRightInset, 0, leftRightInset) 
    } 
} 

GalleryItemCollectionViewCell.swift

class GalleryItemCollectionViewCell: UICollectionViewCell { 
    @IBOutlet var itemImageView: UIImageView! 

    func setGalleryItem(imageURL:String) { 
     //itemImageView.image = #imageLiteral(resourceName: "tempImage2") 
     let url = URL(string: imageURL) 
     DispatchQueue.global(qos: DispatchQoS.QoSClass.default).async { 
      let data = try? Data(contentsOf: url!) 
      DispatchQueue.main.async(execute: { 
       if let imageData = data { 
        if let image = UIImage(data: imageData) { 
         print("We do have the image") 
         self.itemImageView = UIImageView(image: image) 
        } 
       } 
      }); 
     } 
    } 
} 

私は出力に "私たちはイメージを持っている" ので、ダウンロードが完了すると、データがUIImageであることがわかります。私が行コメントを解除した場合

//itemImageView.image = #imageLiteral(resourceName: "tempImage2") 

をして、次の非同期を削除し、すべてが動作します。

通常のUiImageViewを表示するには、ViewControllerでダウンロードコードを使用すると機能します。

更新:

extension UIImageView { func downloadedFrom(link:String, contentMode mode: UIViewContentMode) { guard let url = NSURL(string: link) else {return} contentMode = mode URLSession.shared.dataTask(with: url as URL, completionHandler: { (data, response, error) -> Void in guard let httpURLResponse = response as? HTTPURLResponse, httpURLResponse.statusCode == 200, let mimeType = response?.mimeType, mimeType.hasPrefix("image"), let data = data, error == nil, let image = UIImage(data: data) else { return } DispatchQueue.main.async(execute: { self.image = image }); }).resume() } } 

と私はcellForItemAtに、このような画像を追加:

私はプロジェクトにUIImageViewに拡張子を追加することになった

cell.itemImageView.downloadedFrom(link: cell.photo.image, contentMode: .scaleAspectFill) 

答えて

0
if let image = UIImage(data: imageData) { 
        print("We do have the image") 
        self.itemImageView = UIImageView(image: image) 
       } 

に変更する

if let image = UIImage(data: imageData) { 
        print("We do have the image") 
        self.itemImageView.image = image 
       } 
関連する問題