1

表示されません。私は迅速かつ迅速なネットワーキングには新しいです。セル内の画像は、私はJSONファイルからURLの画像を取得するためにalamofireを使用して、私はセル内のImageViewのにJSONから取得した画像を表示したいんだ

MainCollectionViewControllerに私のコード:

private let reuseIdentifier = "Cell" 

class MainCollectionViewController: UICollectionViewController { 

var result:String = "" 

override func viewDidLoad() { 
    super.viewDidLoad() 



} 
override func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int { 
    // #warning Incomplete implementation, return the number of sections 
    return 1 
} 


override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 
    // #warning Incomplete implementation, return the number of items 
    return 1 
} 

override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell { 
    let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! MainCollectionViewCell 

    Alamofire.request(.POST, "URL That Contain JSON").responseJSON { response in 

     if let value = response.result.value { 

      let json = JSON(value) 

      let data = json["data"].arrayValue 

      self.result = data[0]["image"].stringValue 

      print(self.result) 

     } 
    } 

    let imageName = (result) 
    cell.mainImageView.image = UIImage(named:imageName) 

    return cell 
} 

と画像コンセントがMainCollectionViewCellである:

class MainCollectionViewCell: UICollectionViewCell { 
    @IBOutlet weak var mainImageView: UIImageView! 
} 

ビルドがsuccededが、画像が表示されません、それは内部のイメージなしセルを示しています。

答えて

0

レスポンスが異なるスレッドで実行されているので、その時点で「結果」にはデータがありません。応答があなたの問題を解決した後にロードしようとします。

override func viewDidLoad() { 
    super.viewDidLoad()  
    getImage() 
} 
func getImage() 
{ 
    Alamofire.request(.POST, "URL That Contain JSON").responseJSON { response in 

    if let value = response.result.value { 

     let json = JSON(value) 

     let data = json["data"].arrayValue 

     self.result = data[0]["image"].stringValue 

     print(self.result) 

    } 
//reload collection view 
} 

override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell { 

let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! MainCollectionViewCell 
    let decodedData = NSData(base64EncodedString: result, options: NSDataBase64DecodingOptions(rawValue: 0)) 
        let decodedimage = UIImage(data: decodedData!) 

cell.mainImageView.image = decodedimage 
return cell 
} 
+0

スレッドを取得する1:signal SIGABRT –

+0

あなたは問題を解決しましたか? –

0

あなたのコードでは、あなたのペン先を登録しませんでした。

self.collectionView.registerNib(UINib(nibName: "MainCollectionViewCell", bundle: nil), forCellReuseIdentifier: "MainCollectionViewCell") 

ViewDidLoad

2

にこのコードを入れてAlamofireはまだJSONのために要求しているが、応答が到着する前に、あなたのUIImageViewを更新しました。 Alamofireが応答に戻って来ることができる前に、あなたのコードは、空のUIImageViewを得ている理由はまだあなたがtop.Thatで宣言と同じように、まだ空の文字列あなたResult列を作っ受信されていない結果とUIImageViewが更新しようとします。

override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell { 
let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! MainCollectionViewCell 

Alamofire.request(.POST, "URL That Contain JSON").responseJSON { response in 
    if let value = response.result.value { 
     let json = JSON(value) 
     let data = json["data"].arrayValue 
     self.result = data[0]["image"].stringValue 
     print(self.result) 
     let imageName = (result) 
     cell.mainImageView.image = UIImage(named:imageName) 
    } 
} 
return cell 
} 

ただし、セルが更新されているときにロード要求を行うのは一般的ではありません。

+0

はまだ動作しません。前のような空白のセル –

+0

私はあなたがそのイメージのURL文字列を取得していると仮定します。その場合は、NSDataを使用してurlをimageに変更します。また、UIImage + AfNetworking.swiftクラスを使用することもできます。このクラスをダウンロードすると、URLからイメージを取得できます。 –

0

あなたはちょうどあなたが、その後mainImageViewに設定されたダウンロードのイメージでなければなりませんので、まずサーバから画像のURLではない画像を取得しています。

関連する問題