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
}
任何人都有这方面的想法