- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我一直在尝试从服务器 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/
我需要做一个 POST请求带有 JSON 的 HTTP 正文对象,但我还需要在同一个请求中使用 url 查询参数。 POST: http://www.example.com/api/create?pa
这里是原始的 github issue , 支持者建议在这里开一个帖子以获得更多公众支持。 我正在使用 xcode 6.4。 $ pod --version 0.38.2 我的播客文件: platfo
我在项目中使用cocoapods安装alamofire,它在模拟器中运行良好。当我想在 iphone 6s(ios 13.3.1) 上运行我的应用程序时,它崩溃并显示错误消息。我的 Xcode 版本是
如何取消 Alamofire 共享管理器中的所有请求? 这是我的功能: class func cancelAllRequests() { Alamofire.Manager.sharedIns
我想有多个适配器到同一个 SessionManager,这可能吗? 我的用例是: 从适配器设置默认 header 如果是 basic_auth:添加 basic_auth_adapter 如果是tok
调用 Alamofireobject 映射器的正确方法吗? 有人对我的问题提出建议吗? 最佳答案 func postRequestSample() { let
我是 IOS 的新手,这是我的第一个项目,我想在我的项目中使用 Alamofire 库 我按照所有步骤使用 Cocoapods 安装库,一切都应该正常工作,但我收到这个错误“No这样的模块'Alamo
我是 Swift 的新手,来自 Alamorefire Referencee , 你可以做以下的请求 Alamofire.request(.GET, "http://httpbin.org/get")
默认情况下,Alamofire 发送一个包含 gzip 的 Accept-Encoding header 。我如何告诉它停止这样做?我确实接受 gzip,我很高兴 Alamofire 为我解析它,但是
AFImage 的新功能。我不确定这是否是获取图像并将其缓存的正确方法。似乎每次运行时它都没有访问服务器,但我不确定它是否被缓存了?我走运了?看来我在下面使用的语法也过时了...... 任何评论表示赞
我检查了新的 Alamofire 安装步骤。 由于我需要针对 iOS 7.0,我想知道是否导入 Alamofire.swift是否足以让它工作? 为什么文档声明将函数包裹在 Struct Alamof
在 Alamofire 5 Beta 中,SessionManager已被 Session 取代. 我想知道现在分配 RequestAdapter 的过程是什么,因为这是一个 var在 Alamofi
任何人都可以提出一些关于如何使用 header 扩展 alamofire 的建议,例如需要在发送之前设置的 Content-MD5? 最佳答案 这是一个有点老的问题,但我遇到了同样的问题,我使用以下代
有没有人看到用指纹而不是公钥来固定 Alamofire 的方法? 对不起,如果这已经得到回答,我还没有在任何地方看到它。 谢谢 最佳答案 这最终变得非常简单。下面的代码可能并不完美,我的真实代码正在做
我想运行一个 Alamofire 请求,该请求使用先前 Alamofire 请求的结果作为参数。为了简单起见: //Code1 Alamofire.request("URL", met
当我尝试在应用程序中的 Xcode 中运行我的 iOS 应用程序时: dyld: Library not loaded: @rpath/Alamofire.framework/Alamofire
我想运行一个使用先前 Alamofire 请求的结果作为参数的 Alamofire 请求。为简单起见: //Code1 Alamofire.request("URL", method:
更新:我解决了这个问题。请在下面查看我的回答(在问题和评论下方)。 这个问题被标记为重复,但它是不同的,因为它是一个全新的错误,我无法通过任何搜索找到它。 我尝试将 Alamofire 安装到我的 X
我想先 POST 到一个网站,然后 GET 另一个网站获取数据。 并更改 POST 中的 key ,并在 for 循环中继续这样做 5 次以获得不同的数据。 但是,程序总是先运行 POST 5 次,然
我正在使用 Alamofire 5(测试版 1)为 WooCommerce 编写 API 客户端,这将允许我获取订单、优惠券等以及创建它们。注意我使用的是新的 .responseDecodable功能
我是一名优秀的程序员,十分优秀!