Ios 单元格中的图像不显示
我使用alamofire从json文件获取url图像,并希望在单元格中显示从json获取的图像到imageview。我不熟悉swift和swift网络 MainCollectionViewController上的我的代码:Ios 单元格中的图像不显示,ios,swift,uicollectionview,uicollectionviewcell,alamofire,Ios,Swift,Uicollectionview,Uicollectionviewcell,Alamofire,我使用alamofire从json文件获取url图像,并希望在单元格中显示从json获取的图像到imageview。我不熟悉swift和swift网络 MainCollectionViewController上的我的代码: private let reuseIdentifier = "Cell" class MainCollectionViewController: UICollectionViewController { var result:String = "" override f
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!
}
生成成功,但图像未显示,显示的单元格中没有图像。响应在不同线程中运行,因此当时“结果”没有数据。尝试在响应后加载将解决您的问题
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
}
在代码中,您没有注册nib
self.collectionView.registerNib(UINib(nibName: "MainCollectionViewCell", bundle: nil), forCellReuseIdentifier: "MainCollectionViewCell")
将此代码放到
viewdiload
Alamofire仍在请求JSON,但您的UIImageView
已在响应到达之前更新。在Alamofire
返回响应之前,您的代码将尝试使用尚未收到的结果更新UIImageView
,使您的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
}
然而,当单元格更新时发出加载请求并不是一种流行的做法。您只是从服务器获取图像url而不是图像,因此首先应该下载图像,然后在mainImageView中进行设置。我假设您获取的是该图像的url字符串。如果是,请通过NSData将url更改为图像。您也可以使用UIImage+AfNetworking.swift类,您可以下载该类以从url获取图像作为响应。