ios - ALAMOFIRE 发布请求图像下载和集合视图中的绑定
问题描述
Helo,我是 ios 开发新手,请指导我,我创建了一个带有参数的 alamofire 发布请求,我没有任何问题。之后我需要从生成的 Json 响应中下载图像 url,我不知道如何从 json 下载图像 url 并将其保存在数组中,之后必须从 url 下载 inage 需要将其绑定在图像中集合视图的视图。我不知道如何从 json 响应中获取图像 url,然后将其保存在数组中,然后必须下载图像并将其绑定到集合视图中。请帮我
我已经更新了我在项目中编写的 api 类,它从 url 正确获取数据,我不知道如何将其解析为集合视图单元格图像
func apiclass(){
if(globalConstants.prefs.value(forKey: "authToken") == nil){
self.httpObj.getAuthToken()
}
else if(globalConstants.prefs.value(forKey: "authExpireDate") != nil && Date().compare((globalConstants.prefs.value(forKey: "authExpireDate") as? Date)!) == ComparisonResult.orderedDescending){
globalConstants.prefs.setValue(nil, forKey: "authToken")
self.httpObj.getAuthToken()
}
DispatchQueue.main.async(execute: {
if(globalConstants.prefs.value(forKey: "authToken") != nil){
var request:NSMutableURLRequest = NSMutableURLRequest(url: globalConstants.tokenUrl as URL)
let authToken:String = (globalConstants.prefs.value(forKey: "authToken") as? String)!
request = NSMutableURLRequest(url: globalConstants.tokenUrl as URL)
request.addValue("Bearer \(authToken)", forHTTPHeaderField: "Authorization")
request.addValue(UIDevice.current.identifierForVendor!.uuidString, forHTTPHeaderField: "X-Device-ID")
request.addValue((globalConstants.prefs.value(forKey: "UUID") as? String)!, forHTTPHeaderField: "X-Unique-ID")
request.addValue((globalConstants.prefs.value(forKey: "countryCode") as? String)!, forHTTPHeaderField: "X-Country-Code")
request.addValue((globalConstants.prefs.value(forKey: "currencyCode") as? String)!, forHTTPHeaderField: "X-Currency-Code")
request.addValue((globalConstants.prefs.value(forKey: "currencySymbol") as? String)!, forHTTPHeaderField: "X-Currency-Symbol")
request.addValue(String(stringInterpolationSegment: (globalConstants.prefs.value(forKey: "currencyFactor"))!), forHTTPHeaderField: "X-Currency-Factor")
request.addValue((globalConstants.prefs.value(forKey: "appSource") as? String)!, forHTTPHeaderField: "X-App-Source")
request.addValue(globalConstants.appVersion, forHTTPHeaderField: "X-App-Version")
request.addValue(String(stringInterpolationSegment: (globalConstants.prefs.value(forKey: "estoreId") as? Int)!), forHTTPHeaderField: "X-EStore-ID")
request.addValue("application/json", forHTTPHeaderField: "Content-Type")
request.addValue("application/json", forHTTPHeaderField: "Accept")
if(globalConstants.prefs.value(forKey: "userEmail") != nil){
request.addValue((globalConstants.prefs.value(forKey: "userEmail") as? String)!, forHTTPHeaderField: "X-Email")
}
if(globalConstants.prefs.value(forKey: "userId") != nil){
request.addValue(String(stringInterpolationSegment: (globalConstants.prefs.value(forKey: "userId"))!), forHTTPHeaderField: "X-User-ID")
}
print(self.apiRequestParameters)
var requestParameters:NSMutableDictionary = NSMutableDictionary()
requestParameters = [
"mode" : "design",
"productGroup" : 0
]
print(requestParameters)
request.url = globalConstants.ESDCUrl as URL
self.httpObj.sendRequest("POST", requestDict: requestParameters, request: request as URLRequest, completion: { (result, error) in
print(result!)
if(error == nil){
// move to shopping cart
// dictResult = try NSJSONSerialization.JSONObjectWithData(data!, options: []) as! NSDictionary
self.dictResult = result as! NSArray
self.categorycollectionview.reloadData()
//self.collectionView.reloadData()
// 让 requestDataDict:NSDictionary = incomingRequetArray[0] as! NSDictionary // 让 newDict: NSURL = requestDataDict.object(forKey: "ImageUrl") as? NSDictionary //
}
else{
let alert = UIAlertView()
alert.isHidden = true
alert.addButton(withTitle: "OK")
alert.message = globalConstants.apiErrorMessage
alert.show()
}
})
}
})
}
我已经更新了我的帖子请求的 json 响应。请给我一个解决方案。我可以在控制台中得到以下响应。
( {
ActualPrice = 0;
AssuredTitle = "<null>";
CashBackAmount = 0;
CashBackDiscountPercentage = 0;
Code = "";
Cost = "<null>";
CreateDate = "<null>";
CustomSize = 0;
Delivery = "<null>";
DeliveryDate = "<null>";
DeliveryDays = "<null>";
Designer = "<null>";
Discount = "<null>";
DiscountCost = "<null>";
DispatchDateDisplay = "<null>";
Exclusive = "<null>";
Gender = "<null>";
HasReadyMadeColor = 0;
HasReadyMadeSizes = 0;
ID = 41;
ImageUrl = "https://images.cbazaar.com/images/aqua-blue-georgette-abaya-isbs1805991-u.jpg";
Is7DD = "<null>";
IsCustomizable = 0;
IsExclusive = 0;
IsFree = "<null>";
IsFreeShipping = 0;
IsInWishlist = 0;
IsLookUp = 0;
IsNew = 0;
IsProductAnimation = 0;
IsReadyToShip = 0;
LargeImageUrl = "<null>";
ListingImage = "<null>";
LowerLargeImage = "<null>";
LowerThumbImage = "<null>";
MoneyFactor = "<null>";
MoneyOffFactor = "<null>";
Name = "Islamic wear";
OfferImageUrl = "<null>";
OldPrice = 0;
OutOfStock = 0;
PDUrl = "<null>";
ProductBigLink = "<null>";
ProductCollectionReference = "<null>";
ProductHisImage = "<null>";
ProductSaleCycle = 0;
ProductType = 168;
ProductV2VRatio = 0;
PromotionTypeID = "<null>";
Promotions = "<null>";
PromotionsIcon = "<null>";
Rating = "<null>";
SalePrice = "<null>";
ShowOldPrice = 0;
SizeChart = "<null>";
SuperClassification = "<null>";
TypeGroup = "<null>";
TypeShortCode = "<null>";
UpperLargeImage = "<null>";
UpperThumbImage = "<null>";
VendorType = "<null>";
VisitCount = 0;
})
解决方案
尝试这个
class dataModel{
let profImage: String?
let name: String?
init(profImg: String,name: String)
self.profImage = profImg
self.name = name
}
在 CollectionViewController
let arrayModel = [dataModel]()
After you got the image url add data to array
let dataAll = (dataModel[profImg: YourImageVariable, name: yourNameVariable])
arrayModel.append(dataAll)
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
if let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "YourCellname", for: indexPath) as? yourCell {
let dataIndex = dataModel[indexPath.row]
cell.updateUI(setMdata: dataIndex)
return cell
}
}
在单元格类中导入 SDWebImage
class myCell: UICollectionViewCell {
@IBOutlet weak var name: UILabel!
@IBOutlet weak var profImage: UIImageView!
func updateUI(setMdata: dataModel) {
name.text = dataModel.name
profImagesd._setImage(with: URL(string:dataModel.profImg), placeholderImage: nill)
}
}
推荐阅读
- javascript - Typescript - 在另一个对象中检索一个对象
- javascript - 在javascript中匹配指定的Regex后如何在单独的变量中获取字符串
- android - kivy android AttributeError:'NoneType'对象没有属性'play'
- c++ - CMake 管理本地依赖项
- c# - 获取每个 xml 节点的 XmlNode 值
- python - Gensim 的相似之处
- python - 如何将传感器数据写入单独的文本文件(Raspberry Pi,Python)
- javascript - 引导定价示例 - 我的代码做错了什么?
- dask - 在 PythonScriptStep 中使用 Dask 集群
- excel - 匹配列中单元格的第一个实例不等于具有偏移量的另一列