gpt4 book ai didi

swift - 如何使用 alamofire 从 json 数组中检索图像?

转载 作者:可可西里 更新时间:2023-11-01 01:57:49 24 4
gpt4 key购买 nike

我一直在尝试从服务器 URL 获取产品的详细信息和图像。我能够获取详细信息但不能获取图像,因为图像是一个 JSONarray。请帮我检索查看代码的图像。顺便说一句,我使用了“Alamofire”库。非常感谢!

下面的 JSON:

 {
"products": [
{
"product_id": 2,
"name": "Ring",
"description": "<p> </p>",
"price": 674,
"master_variant_id": 2,
"cost_currency": "USD",
"shipping_and_returns": "",
"city_or_state": null,
"state_or_province": null,
"user": {
"company_name": "Gift Shop",
"firstname": "giftvendor"
},
"master_variant_images": [
"https://s3.amazonaws.com/webdevapp/app/public/spree/products/2/product/ri1.jpg?1476104874"
],
"variants": [],
"product_url": "http://staging.giftintime.com/mobile/products/2"
}
}

下面是我的代码

var gifther = [Gifther]()

override func viewDidLoad() {
super.viewDidLoad()
tableview.delegate = self
tableview.dataSource = self

Alamofire.request("http://staging.giftintime.com/mobile/categories/2/subcategories").responseJSON { (response) in

let result = response.result

if let dict = result.value as? Dictionary <String, Any> {

if let giftherdata = dict["products"] as? [Dictionary<String, Any>] {

for obj in giftherdata {

let giftintime = Gifther(giftherData: obj)
self.gifther.append(giftintime)
//print(giftintime)
}
self.tableview.reloadData()
}
}
}
}
public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = self.tableview.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! giftherTableViewCell

let Gift: Gifther
Gift = gifther[indexPath.row]

//Displaying values
cell.productnameLbl.text = Gift.productname
cell.companynameLbl.text = "By " + Gift.companyname
cell.priceLbl.text = Gift.price
cell.currencytypeLbl.text = Gift.currencytype

//cell.imageview?.image = UIImage(named: Gift.imagesUrl)

// cell.selectionStyle = UITableViewCellSelectionStyle.none

//Displaying image



Alamofire.request(Gift.imagesUrl ).responseImage { (response) in
debugPrint(response)

if let image = response.result.value {

cell.imageview.image = image

}

}

/*

Alamofire.request(Gift.imagesUrl as! URLRequestConvertible).responseData { (response) in
if response.error == nil {
print(response.result)
}

if let data = response.data {
cell.imageview.image = UIImage(data: data)
}
}
*/
return cell
}

下面是我在另一个类(class)的代码

class Gifther {

var _productname:String?
var _price:String?
var _currencytype:String?
var _companyname:String?
var _imagesUrl:String?
var _productUrl:String?

//product details
var _descriptionData: String?
var _shippingAndreturns :String?

var productname:String {

if _productname == nil {
_productname = ""

}
return _productname!
}

var price:String {

if _price == nil {
_price = " "

}
return _price!
}

var currencytype:String {

if _currencytype == nil {
_currencytype = ""
}
return _currencytype!
}

var companyname:String {

if _companyname == nil {
_companyname = ""
}
return _companyname!
}

var imagesUrl:String {

if _imagesUrl == nil {
_imagesUrl = ""
}
return _imagesUrl!
}

//商品详情

var productUrl:String {

if _productUrl == nil {
_productUrl = ""
}
return _productUrl!
}

var descriptionData:String {

if _descriptionData == nil {
_descriptionData = ""
}
return _descriptionData!
}

var shippingAndreturns:String {

if _shippingAndreturns == nil {
_shippingAndreturns = ""
}
return _shippingAndreturns!
}



init(giftherData: Dictionary<String , Any>) {

if let name = giftherData["name"] as? String {

self._productname = name
}

if let Price = giftherData["price"] as? Float {
self._price = String(describing: Price)
}

if let moneyType = giftherData["cost_currency"] as? String {

self._currencytype = moneyType
}

if let companyname = giftherData["user"] as? Dictionary<String, Any> {

if let name = companyname["company_name"] as? String {

self._companyname = name
}
}


if let image = giftherData["master_variant_images"] as? NSArray {

print("images are : \(image.count)")

self._imagesUrl = String(describing: image)

}

//Product details

if let Producturl = giftherData["product_url"] as? String {

self._productUrl = Producturl

}

if let description = giftherData["description"] as? String {

self._productUrl = description
}

if let shippingAndreturns = giftherData["shipping_and_returns"] as? String {

self._shippingAndreturns = shippingAndreturns
}
}

}

最佳答案

试试这个。

    Alamofire.request(URL.init(string: "https://s3.amazonaws.com/webdevapp/app/public/spree/products/2/product/ri1.jpg?1476104874")! ).responseData { (response) in

//debugPrint(response)

if let image = response.result.value {

let img = UIImage.init(data: image)

cell.imageView?.image = img

}
}

替换这个 alamofire 方法希望这有帮助

关于swift - 如何使用 alamofire 从 json 数组中检索图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49919467/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com