I had no idea how to parse JSON data using Alamofire. Right now I successfully request the data from web service. The problem is I'm not really sure how to embed/parse json (image) data into UICollectionView
import UIKit
import Alamofire
class PopularViewController: UIViewController,UICollectionViewDataSource, UICollectionViewDelegate {
var users: [AnyObject] = []
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
//#warning Incomplete method implementation -- Return the number of items in the section
return users.count
}
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier("UserCell", forIndexPath: indexPath) as PopularCollectionViewCell
Alamofire.request(.GET, "http://xxxxx/users.json").responseJSON { (_, _, data, _) in
println(data)
}
return cell
}
override func viewDidLoad() {
super.viewDidLoad()
}
}
Json Data
{
"users": [{
"userId": 1,
"profilePhoto": "https://graph.facebook.com/1301454197/picture?type=large"
}]
}
Please advice. Thank you.
First, I am assuming you have some UIImageView inside your UICollectionView or else you will need that first.
Once you parse the json data and have the image urls, you need to use another library to download the actual image. I have found SDWebImageDownloader (https://github.com/rs/SDWebImage) being useful for this task. This library is in objective C so you have to use the bridging header.
The other one being (https://github.com/Haneke/HanekeSwift). I have not personally used it but it looks like this is what you do. However, this one is in swift so it might be easier for you to try this one first. From the documentation, it looks like they extend UIImageView so you should have these methods available.
I hope this helps.
@Dato' Mohammed Nurdin This will work if you use Swiftyjson, if you want to download the image, I'd recommend you to extend the Request in the Alamofire framework and build your own responseImage function like this:
I already found the solution.
PopularViewController.swift
PopularCollectionViewCell.swift