从服务器端到UICollectionView的映像

时间:2016-06-01 15:31:51

标签: ios swift uicollectionview

var expoImage: [String] = []

我已经从mySQL数据库及其所有内容中获取了值,其中一个是图像的URL链接,并将其保存在同一个Web主机中

func loadData() {

    let url = NSURL(string: "http://www.q8-9ndo5aan.com/kuwaitExpoApps/script/getExpo.php")
    let request = NSMutableURLRequest(URL: url!)

    // modify the request as necessary, if necessary

    NSURLSession.sharedSession().dataTaskWithRequest(request, completionHandler: { (data:NSData?, response:NSURLResponse?, error:NSError?) -> Void in

        dispatch_async(dispatch_get_main_queue(), { 

            if error != nil {
                // Display an alert message

                print(error)

                return
            }

            do {

                let json = try NSJSONSerialization.JSONObjectWithData(data!, options: .MutableContainers) as? [[String:AnyObject]]


                if (json != nil) {

                    for item in json! {

                self.expoImage.append((item["expoImage"] as? String)!)

                        self.myCollectionView.reloadData()

                    }

                    // Display an alert message


                } else {

                    // Display an alert message
                    let userMessage = "Could not fetch Value"
                    print(userMessage)

                }



            } catch  {

                print(error)

            }

        })

    }).resume()


}

当我在Xcode的控制台中打印时,链接显示得很好,但我的问题是没有在UICollectionView中显示图像

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

    let myCell: myCollectionViewCell = collectionView.dequeueReusableCellWithReuseIdentifier("myCell", forIndexPath: indexPath) as! myCollectionViewCell
   dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), {

        let imageString = self.expoImage[indexPath.row]
        let imageUrl = NSURL(string: imageString)
        let imageData = NSData(contentsOfURL: imageUrl!)

        dispatch_async(dispatch_get_main_queue(), {

            if(imageData != nil) {

                myCell.expoImage.image = UIImage(data: imageData!)
                myCell.expoImageBack.image = UIImage(data: imageData!)

            }

        });

    });

    return myCell

}

任何人都有这方面的想法

1 个答案:

答案 0 :(得分:0)

这可能是因为新的App Transport Security会阻止所有非HTTPS请求。您链接可能是常规HTTP链接,导致它返回'nil'。如果是这种情况,那么您需要修改您的应用程序的info.plist。确保您的info.plist看起来像这样 -

enter image description here