Ios 如何在CollectionView中显示API中的图像
因此,我需要解析来自JSON的图像,并在Ios 如何在CollectionView中显示API中的图像,ios,swift,uicollectionview,alamofire,swifty-json,Ios,Swift,Uicollectionview,Alamofire,Swifty Json,因此,我需要解析来自JSON的图像,并在CollectionView中显示它们。我使用了一些类似的框架:Alamofire、AlamofireImage、SwiftyJson My JSON is here: MyCollectionViewCell: import UIKit class CollectionViewCell: UICollectionViewCell { @IBOutlet weak var imageView: UIImageView!
CollectionView
中显示它们。我使用了一些类似的框架:Alamofire、AlamofireImage、SwiftyJson
My JSON is here:
MyCollectionViewCell
:
import UIKit
class CollectionViewCell: UICollectionViewCell {
@IBOutlet weak var imageView: UIImageView!
}
导入UIKit
类CollectionViewCell:UICollectionViewCell{
@IBVAR imageView:UIImageView!
}
请帮帮我。谢谢 您可以使用
map
函数迭代data
数组中的每个对象,并从images
数组下的fixed\u height\u small
对象中提取url
。(假设您需要从fixed\u height\u small
中提取url
)
您的json字典在解析后如下所示:
["data":
[
["images":
["fixed_height_small":
["url": "http:\/\/media3.giphy.com\/media\/Un5PeP1wG99QI\/100.gif"]
]
]
]
]
示例代码:
func loadImages() {
Alamofire.request("http://api.giphy.com/v1/gifs/trending?api_key=dc6zaTOxFJmzC")
.responseJSON {(responseData) -> Void in
if ((responseData.result.value) != nil) {
let responseJsonData = JSON(responseData.result.value!)
print(responseJsonData)
self.imagesArray = responseJsonData["data"].arrayValue.map({
(item) -> NSURL in
NSURL(string: item["images"]["fixed_height_small"]["url"].stringValue)!
})
if self.imagesArray.count > 0 {
self.myCollectionView.reloadData()
}
}
}
}
您可以使用在imageview中加载url 下面是相同的示例代码:
let gifURL : String = "http://api.giphy.com/v1/gifs/trending?api_key=dc6zaTOxFJmzC"
let imgURL = UIImage.gifImageWithURL(gifURL)
let imgvwTemp = UIImageView(image: imageURL)
cell.imageView.image = imgvwTemp.image
希望它能帮助您:)您的代码有什么问题above@Enix在函数loadImage()中,我接下来要编写:self.imagesArray=resData as!我正在编写的cellForItemAt函数中的[[String:AnyObject]]中:让url=NSURL(String:imagesArray[indexPath.row]),我得到错误:“无法将类型为“[String:AnyObject]”的值转换为预期的参数类型“String”
func loadImages() {
Alamofire.request("http://api.giphy.com/v1/gifs/trending?api_key=dc6zaTOxFJmzC")
.responseJSON {(responseData) -> Void in
if ((responseData.result.value) != nil) {
let responseJsonData = JSON(responseData.result.value!)
print(responseJsonData)
self.imagesArray = responseJsonData["data"].arrayValue.map({
(item) -> NSURL in
NSURL(string: item["images"]["fixed_height_small"]["url"].stringValue)!
})
if self.imagesArray.count > 0 {
self.myCollectionView.reloadData()
}
}
}
}
let gifURL : String = "http://api.giphy.com/v1/gifs/trending?api_key=dc6zaTOxFJmzC"
let imgURL = UIImage.gifImageWithURL(gifURL)
let imgvwTemp = UIImageView(image: imageURL)
cell.imageView.image = imgvwTemp.image