gpt4 book ai didi

ios - 从 alamofire swift 从 tableview 单元格中的 url 加载图像

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

我正在尝试使用 Alomofire 和 swiftyJSON 从 json 加载图像。 Json是字典:

{"name": {"image": "https://...",}}

Alamorefire 和 SwiftyJSON

var imageLoad: String!

Alamofire.request(.GET, url).responseJSON { (response) -> Void in
if let value = response.result.value {
let json = JSON(value)

if let jsonDict = json.dictionary {

let image = jsonDict["name"]!["image"].stringValue
print(image) // https://....
self.imageLoad = image
}
self.tableView.reloadData()
}
}

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

// Configure the cell...

cell.imageTableView.image = UIImage(named: imageLoad) // not working "fatal error: unexpectedly found nil while unwrapping an Optional value"

如果有人能帮忙?如果还有其他方法,请随意编写。

最佳答案

我分享了使用 Swift 3AlamofireImage 实现的代码:

import UIKit
import AlamofireImage

class MovieViewCell: UICollectionViewCell {

@IBOutlet weak var titleLabel: UILabel!
@IBOutlet weak var overviewLabel: UILabel!
@IBOutlet weak var posterView: UIImageView!

func configure(for movie: Movie) {
titleLabel.text = movie.title
overviewLabel.text = movie.overview

let url = URL(string: movie.poster_path!)!
posterView.af_setImage(withURL: url)
}

override func prepareForReuse() {
super.prepareForReuse()

posterView.af_cancelImageRequest()
posterView.image = nil
}
}

此外,您还需要 Cocoapods:

platform :ios, '10.0'

inhibit_all_warnings!
use_frameworks!

target 'MovieApp' do
pod 'Alamofire', '~> 4.5'
pod 'AlamofireImage', '~> 3.3'
end

官方文档AlamofireImagean ImageCell.swift example

关于ios - 从 alamofire swift 从 tableview 单元格中的 url 加载图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35854764/

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