I am trying to load image from json using Alomofire and swiftyJSON. Json is dictionary:
{"name": {"image": "https://...",}}
Alamorefire and 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"
If anyone can help? If there is another way feel free to write.
I share a code implemented using Swift 3 and AlamofireImage:
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
}
}
Also, you will need Cocoapods:
platform :ios, '10.0'
inhibit_all_warnings!
use_frameworks!
target 'MovieApp' do
pod 'Alamofire', '~> 4.5'
pod 'AlamofireImage', '~> 3.3'
end
Official documentation AlamofireImage and an ImageCell.swift example
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With