gpt4 book ai didi

ios - 调用在 UITableview 中显示图像时图像 url 显示错误

转载 作者:行者123 更新时间:2023-11-30 13:43:43 24 4
gpt4 key购买 nike

我已将 json 图像 url 存储在数组中,但在调用该 url 时显示错误

模型类

import Foundation
class ProductDetails {
var productAuthor: String!
var productPrice: Int!
var artImages: [ArtImage]!
}

class ArtImage {
var imagepath: String!
var imgvideotype: Int!
}

TableView Controller 存储变量

var globalArr = [ProductDetails]()

解析函数

func parseJSONData(data: NSData) -> [ProductDetails] {
var product_Detail = [ProductDetails]()
do {
let jsonResult = try NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers) as? NSDictionary

let jsonProductDetails = jsonResult?["data"] as! [AnyObject]
//print("the json response is",jsonProductDetails)

for jsonproductDetail in jsonProductDetails{
let productDetail = ProductDetails()
// let jsonProductImageDetails = jsonProductDetails["images"] as! [AnyObject]

productDetail.productAuthor = jsonproductDetail["first_name"]as! String
productDetail.productPrice = jsonproductDetail["prodprice"]as! Int


// Getting inside the json
let jsonProductImageDetails = jsonproductDetail["images"] as! [AnyObject]
var artImagesModelArray = [ArtImage]()
for image in jsonProductImageDetails {
let artImage = ArtImage();
artImage.imagepath = image["imagepath"] as! String
artImage.imgvideotype = image["imgvideotype"] as! Int
artImagesModelArray.append(artImage)
}
productDetail.artImages = artImagesModelArray;
product_Detail.append(productDetail)



}

}

catch {
print (error)
}

return product_Detail
}

表格 View 数据源

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cellIdentifier = "Cell"
let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) as! UserHomeScreenTableViewCell

// Configure the cell...
cell.artsAuthorName.text = globalArr[indexPath.row].productAuthor
cell.priceLabel.text = "\(globalArr[indexPath.row].productPrice)"
let productDetailsObject = globalArr[indexPath.row].artImages
print("@@@@@@@@@@@@@@@@@",productDetailsObject)


dispatch_async(dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), {

if let url = (NSURL(string: self.globalArr[indexPath.row])) {

//The error comes here....in self.global array

if let data = NSData(contentsOfURL: url) {
if let image = UIImage(data: data) {
dispatch_async(dispatch_get_main_queue()) { () -> Void in
cell.artImageView.image = image
}
}
}
}
})



return cell
}

这里我存储了解析 json 并在 tableview 单元格中显示一些详细信息,到目前为止一切正常。 但调用异步方式从数组加载图像时显示错误

任何建议..plz..谢谢

最佳答案

您需要更改从 ProductDetails 获取图像 url 的方式。我想你需要使用这样的东西:

 if let url = NSURL(string: self.globalArr[indexPath.row].artImages[imageIndex].imagepath) { // your code }

因为当你表演

if let url = (NSURL(string: self.globalArr[indexPath.row]))

您获得了 ProductDetails 对象,但没有获得图像 url。

关于ios - 调用在 UITableview 中显示图像时图像 url 显示错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35239608/

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