Swift tableView slow and scroll lag - ios

I am Pulling data from a json API Call the table view populates but is very slow and lags when scrolling down how do i speed up the loading of the table. I am new to swift and xcode any tips would be appreciates
import Foundation
import UIKit
class featuredViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
#IBOutlet weak var tableView: UITableView!
#IBOutlet weak var searchBar: UISearchBar!
// Array for JSON Data
var property: [featuredClass.property] = []
var imageArray = [String]()
var imageCollection = [[String]]()
var refreshControl: UIRefreshControl!
override func viewDidLoad() {
super.viewDidLoad()
self.getProperties()
// Do any additional setup after loading the view, typically from a nib.
refreshControl = UIRefreshControl()
refreshControl.attributedTitle = NSAttributedString(string: "Pull to refresh")
refreshControl.addTarget(self, action: #selector(featuredViewController.getProperties), for: UIControlEvents.valueChanged)
tableView.addSubview(refreshControl)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
/***********************************************************************************************/
func getProperties() {
let downloadTask = APICalls.getFeatured()
URLSession.shared.dataTask(with: downloadTask, completionHandler: {(data, response, error) -> Void in
if let httpResponse = response as? HTTPURLResponse {
print("statusCode: \(httpResponse.statusCode)")
}
/******** Parse JSON **********/
do { // A Dictionary of Dictionaries
let jsonObject = try JSONSerialization.jsonObject(with: data!, options: JSONSerialization.ReadingOptions.mutableContainers)
if let jsonDict = jsonObject as? NSDictionary {
// Do smthg.
//print(jsonDict) // Debug the json
let meCount = Int((jsonDict.count)) - 1; //get number to use for our loop
for index in 0...meCount {
for (_, value) in jsonDict { //Turns every key's value into a dictionary
// Fill property struct from json
self.property.append(featuredClass.property.init(jsonDict: value as! NSDictionary))
//print(self.property) // Uncomment for debugging
/** Get Image 0 for featured Image **/
let myData = self.property[index].image
// print(myData ?? "Error")
if myData?["0"] != nil {
let myData2 = myData?["0"] as! NSDictionary
self.imageArray.append(myData2["url"] as! String)
//print(myData2["url"] as! String)
}
else {
self.imageArray.append("\(#imageLiteral(resourceName: "property-placeholder-800x500"))")
}
/* ENd Get image 0 */
}
}
}
}catch {
//...
}
let meCount = (self.property.count)-1
/******** End Parse JSON **********/
//print(meCount)
if meCount != -1 {
}
else {
// Show alert view
let contactAddedAlert = UIAlertController(title: "Error: Check if Access Key is correct",
message: nil, preferredStyle: .alert)
contactAddedAlert.addAction(UIAlertAction(title: "OK", style: .cancel, handler: nil))
self.present(contactAddedAlert, animated: true, completion: nil)
}
/******** Reload table View **********/
OperationQueue.main.addOperation({
self.tableView.reloadData()
self.refreshControl.endRefreshing()
}) }).resume()
}
/***********************************************************************************************/
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return property.count
}
/***********************************************************************************************/
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cellFeatured") as! featuredTableViewCell
cell.addressLabel.text = property[indexPath.row].address
cell.cityNameLabel.text = property[indexPath.row].cityName
let imgURL = NSURL(string: imageArray[indexPath.row])
if imgURL != nil {
let data = NSData(contentsOf: (imgURL as URL?)!)
cell.imgView.image = UIImage(data: data! as Data)
}
return cell
}
}

NSData(contentsOf: (imgURL as URL?)!) is synchronous. Please refer to the SDK document: https://developer.apple.com/reference/foundation/nsdata/1547245-datawithcontentsofurl
Which state that:
Do not use this synchronous method to request network-based URLs. For network-based URLs, this method can block the current thread for tens of seconds on a slow network, resulting in a poor user experience, and in iOS, may cause your app to be terminated.
Instead, for non-file URLs, consider using the dataTaskWithURL:completionHandler: method of the NSURLSession class. See URL Session Programming Guide for details.

You can use this pod to boost your speed
https://github.com/rs/SDWebImage
It will be great to load images.

Related

TableView not updating after button action

I'm relatively new to swift, and I'm trying to have a view that when it loads it will display some info on my tableView, then in the same view I have a textfield and a button
I want that the button performes an action that fetchs data from my server and updates my tableView
The problem is that the table is not being updated. How can I get the table to be updated?
CODE:
override func viewDidLoad() {
super.viewDidLoad()
tableView.rowHeight = 80;
tableView.tableFooterView = UIView();
self.url = URL(string: "http://xxxxxxxx.com/xxxxx/api/produtos/listagem/favoritos/\(userID)");
downloadJson(_url: url!);
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return produtos.count;
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
guard let cell = tableView.dequeueReusableCell(withIdentifier: "ProdutoCell1") as? ProdutoCell else{
return UITableViewCell()
}
cell.lbl_nome_json.text = produtos[indexPath.row].nome;
cell.lbl_categoria_json.text = produtos[indexPath.row].categoria;
cell.lbl_loja_json.text = produtos[indexPath.row].loja
//= produtos[indexPath.row].categoria;
// cell.lbl_loja.text = produtos[indexPath.row].loja;
if let imageURL = URL(string: "http://xxxxxxxxxx.com/myslim/api/"+produtos[indexPath.row].url){
DispatchQueue.global().async {
let data = try? Data(contentsOf: imageURL);
if let data = data{
let image = UIImage(data: data);
DispatchQueue.main.async {
cell.imgView_json.image = image;
}
}
}
}
return cell;
}
ACTION:
#IBAction func btn_search(_ sender: Any) {
if self.txt_search.text == "" {
let alert = UIAlertController(title:"Alert",message: "Insira algo para pesquisar",preferredStyle: UIAlertController.Style.alert)
alert.addAction(UIAlertAction(title: "Click", style: UIAlertAction.Style.default, handler: nil))
self.present(alert,animated: true,completion: nil)
}else{
var pesquisa = String();
pesquisa = self.txt_search.text!;
let url2 = URL(string: "http://xxxxxxxx.com/xxxxxx/api/produtos/listagem/favoritos/\(pesquisa)/\(self.userID)");
downloadJson(_url: url2!);
}
func downloadJson(_url: URL){
guard let downloadURL = url else {return}
URLSession.shared.dataTask(with: downloadURL){data, urlResponse, error in
guard let data = data, error == nil, urlResponse != nil else{
print("algo está mal");
return;
}
do{
let decoder = JSONDecoder();
let downloadedProdutos = try decoder.decode([ProdutoModel].self, from: data);
self.produtos = downloadedProdutos
print(downloadedProdutos);
DispatchQueue.main.async {
self.tableView.reloadData();
print("reload");
}
}catch{
print("algo mal depois do download")
}
}.resume();
}
EDIT
I added some print to see how many object were being returned on my downloadedProdutos variable, on the downloadJson() function.
And at the viewdidload I get 2 object, its normal because I only have 2 Products, but when the action is done I still get 2 object although I should get only 1 object
You need to set tableView.dataSource = self in viewWillAppear and it looks you missed func numberOfSections() -> Int method.
Add UITableViewDataSource like this
class YourViewController: UIViewController, UITableViewDataSource and it will recommend you required methods

How do I fix laggy UITableView scrolling performance when downloading JSON?

In my application, I download a JSON file off of the internet and fill up a UITableView with items from the file. It does work well, and there are no problems or errors, but the scrolling performance is very laggy, and the UI glitches out a tiny bit.
I assume this is because of the images that I'm downloading from the JSON file, so I've looked into multi-threading, but I don't think I am doing it right because it does load much faster, but scrolling performance is still the same as before.
Can somebody please tell me how to fix this? This UITableView is the most important thing in the app, and I have been spending much time on trying to fix it. Thank you!
Here is my code-
import UIKit
class ViewController: UIViewController, UITableViewDataSource {
#IBOutlet weak var tableView: UITableView!
var nameArray = [String]()
var idArray = [String]()
var ageArray = [String]()
var genderArray = [String]()
var descriptionArray = [String]()
var imgURLArray = [String]()
let myActivityIndicator = UIActivityIndicatorView(activityIndicatorStyle: UIActivityIndicatorViewStyle.gray)
final let urlString = "https://pbsocfilestorage.000webhostapp.com/jsonDogs.json"
override func viewDidLoad() {
super.viewDidLoad()
self.downloadJsonWithURL()
// Activity Indicator
myActivityIndicator.center = view.center
myActivityIndicator.hidesWhenStopped = true
myActivityIndicator.startAnimating()
view.addSubview(myActivityIndicator)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func downloadJsonWithURL() {
let url = NSURL(string:urlString)
URLSession.shared.dataTask(with: (url as? URL)!, completionHandler: {(data, response, error) ->
Void in
print("Good so far...")
if let jsonObj = try? JSONSerialization.jsonObject(with: data!, options: .allowFragments) as? NSDictionary {
print(jsonObj!.value(forKey: "dogs"))
if let dogArray = jsonObj!.value(forKey: "dogs") as? NSArray {
print("Why u no work!")
for dog in dogArray {
if let dogDict = dog as? NSDictionary {
if let name = dogDict.value(forKey: "name") {
self.nameArray.append(name as! String)
}
if let name = dogDict.value(forKey: "id") {
self.idArray.append(name as! String)
}
if let name = dogDict.value(forKey: "age") {
self.ageArray.append(name as! String)
}
if let name = dogDict.value(forKey: "gender") {
self.genderArray.append(name as! String)
}
if let name = dogDict.value(forKey: "image") {
self.imgURLArray.append(name as! String)
}
if let name = dogDict.value(forKey: "description") {
self.descriptionArray.append(name as! String)
}
OperationQueue.main.addOperation ({
self.myActivityIndicator.stopAnimating()
self.tableView.reloadData()
})
}
}
}
}
}).resume()
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return nameArray.count
}
func tableView(tableView: UITableView, estimatedHeightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return UITableViewAutomaticDimension;
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let imgURL = NSURL(string: imgURLArray[indexPath.row])
let cell = tableView.dequeueReusableCell(withIdentifier: "reusableCell") as! TableViewCell
URLSession.shared.dataTask(with: (imgURL as! URL), completionHandler: {(data, resp, error) -> Void in
if (error == nil && data != nil) {
OperationQueue.main.addOperation({
cell.dogNameLabel.text = self.nameArray[indexPath.row]
cell.idLabel.text = self.idArray[indexPath.row]
cell.ageLabel.text = self.ageArray[indexPath.row]
cell.genderLabel.text = self.genderArray[indexPath.row]
print("Cell info was filled in!")
if imgURL != nil {
let data = NSData(contentsOf: (imgURL as? URL)!)
cell.dogImage.image = UIImage(data: data as! Data)
}
})
}
}).resume()
return cell
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "showDog" {
if let indexPath = self.tableView.indexPathForSelectedRow{
let detailViewController = segue.destination as! DetailViewController
detailViewController.imageString = imgURLArray[indexPath.row]
detailViewController.nameString = nameArray[indexPath.row]
detailViewController.idString = idArray[indexPath.row]
detailViewController.ageString = ageArray[indexPath.row]
detailViewController.descriptionString = descriptionArray[indexPath.row]
detailViewController.genderString = genderArray[indexPath.row]
}
}
}
}
There is a big mistake. You are loading data with dataTask but you aren't using that returned data at all. Rather than you are loading the data a second time with synchronous contentsOf. Don't do that.
And don't update the labels in the asynchronous completion block. The strings are not related to the image data.
This is more efficient:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let imgURL = URL(string: imgURLArray[indexPath.row])
let cell = tableView.dequeueReusableCell(withIdentifier: "reusableCell", for: indexPath) as! TableViewCell
cell.dogNameLabel.text = self.nameArray[indexPath.row]
cell.idLabel.text = self.idArray[indexPath.row]
cell.ageLabel.text = self.ageArray[indexPath.row]
cell.genderLabel.text = self.genderArray[indexPath.row]
print("Cell info was filled in!")
URLSession.shared.dataTask(with: imgURL!) { (data, resp, error) in
if let data = data {
OperationQueue.main.addOperation({
cell.dogImage.image = UIImage(data: data)
})
}
}.resume()
return cell
}
Note: You are strongly discouraged from using multiple arrays as data source. It's very error-prone. Use a custom struct or class. And create imgURLArray with URL instances rather than strings. This is also much more efficient.
Nevertheless, you should use a download manager which caches the images and cancels downloads if a cell goes off-screen. At the moment each image is downloaded again when the user scrolls and cellForRow is called again for this particular cell.

Retrieving data back from server side based on input

I'm doing a school project, its's multiple tables and tabBarControllers. Now, I want to display return data back from server side based on selected row. I managed to pass selected row from View to another, and I believe there is nothing wrong with passing it to the server side, too. The problem is when retrieving the data back from the server.
Here is Course View
// This array is to hold data coming from server side
var course: NSArray = []
// Assign data that come from DepartmentView
public var department: String? = nil
var myURL = URL(string: "http://localhost/masterProject/scripts/courses.php")
override func viewDidLoad()
{
super.viewDidLoad()
self.title = "Courses"
}
override func viewWillAppear(_ animated: Bool)
{
super.viewWillAppear(animated)
// This is just to make sure it gets data and not nil
let vDepartment = department!
// Create a request
var request = URLRequest(url: myURL!)
request.httpMethod = "POST";
// Create a parameter to be sent to server side and based on this
// data I want to get data back that belong to it.
let postParameter = "department=\(vDepartment)";
request.httpBody = postParameter.data(using: String.Encoding.utf8)
// Here I Create and send dataTask
let task = URLSession.shared.dataTask(with: request)
{ (data: Data?, response: URLResponse?, error: Error?) in
DispatchQueue.main.async
{
if(error != nil)
{
self.alertMessage(message: (error?.localizedDescription)!)
return
}
// This is supposed to read data from server side and display it
// in Course table view.
self.course = try! JSONSerialization.jsonObject(with: data!, options: .mutableContainers) as! NSArray
}
}
task.resume()
}
// MARK: - Table view data source
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
return course.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
let cell = tableView.dequeueReusableCell(withIdentifier: "courseCell", for: indexPath) as! CourseTableViewCell
let main = course[indexPath.row] as! NSDictionary
cell.courseName.text = (main["name"] as! String)
cell.courseNumber.text = (main["number"] as! String)
return cell
}
func alertMessage(message: String)
{
let alert = UIAlertController(title: "Alert", message: message, preferredStyle: .alert)
let action = UIAlertAction(title: "Ok", style: .default, handler: nil)
alert.addAction(action)
self.present(alert, animated: true, completion: nil)
}
Department view
var departments: NSArray = []
var myURL = URL(string: "http://localhost/masterProject/scripts/department.php")
override func viewDidLoad()
{
super.viewDidLoad()
}
override func viewWillAppear(_ animated: Bool)
{
self.title = "Departments"
super.viewWillAppear(animated)
let data = NSData(contentsOf: myURL!)
departments = try! JSONSerialization.jsonObject(with: data! as Data, options: .mutableContainers) as! NSArray
}
// MARK: - Table view data source
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
// #warning Incomplete implementation, return the number of rows
return departments.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
let data = departments[indexPath.row] as! NSDictionary
cell.textLabel?.text = (data["name"] as! String)
return cell
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?)
{
if segue.identifier == "showDepCourses"
{
if let indexPath = tableView.indexPathForSelectedRow
{
let courseVC = segue.destination as! CourseTableViewController
let data = departments[indexPath.row] as! NSDictionary
courseVC.department = (data["name"] as! String)
}
}
}
}
Here are the picture for both Views
department view
course view
I'm supposed get the right data based on selected row but instead it shows nothing.
self.tabeView.reloadData() was missing the whole time.
This is the answer for the question:
override func viewWillAppear(_ animated: Bool)
{
super.viewWillAppear(animated)
// Just to make sure "Optional" keyword not send as POST PERAMETER
let selectedDep = department!
// Create a request
var request = URLRequest(url: myURL!)
request.httpMethod = "POST";
// Create a parameter to be sent to server side and based on this
// data I want to get data back that belong to it.
let postParameter = "department=\(selectedDep)";
request.httpBody = postParameter.data(using: String.Encoding.utf8)
// Here I create and send dataTask
let task = URLSession.shared.dataTask(with: request)
{ (data: Data?, response: URLResponse?, error: Error?) in
DispatchQueue.main.async
{
if(error != nil)
{
self.alertMessage(message: (error?.localizedDescription)!)
return
}
// This is supposed to read data from server side and display it
// in Course table view.
self.courses = try! JSONSerialization.jsonObject(with: data!, options: .mutableContainers) as! NSArray
self.tableView.reloadData()
}
}
task.resume()
}

IOS swift how can I populate my TableView with Json data being returned

I have just started working with Swift and am able to do some basic things. Right now I am trying to populate my UITableView with Json Data that I am successfully retrieving. Right now I have this simple Table that looks like this
That is a basic TableView that I was able to create with this code
#IBOutlet var StreamsTableView: UITableView!
let groceries = ["Fish","lobster","Rice","Beans"]
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
let mycell:UITableViewCell = StreamsTableView.dequeueReusableCell(withIdentifier: "prototype1", for: indexPath)
mycell.textLabel?.text = groceries[indexPath.row]
return mycell
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
return groceries.count
}
override func viewDidLoad() {
super.viewDidLoad()
StreamsTableView.dataSource = self
}
I now have a JsonRequest that I am completing successfully using this code below
override func viewDidLoad() {
super.viewDidLoad()
StreamsTableView.dataSource = self
// Do any additional setup after loading the view.
var names = [String]()
let urlString = "http://localhost:8000/streams"
let url = URL(string: urlString)
URLSession.shared.dataTask(with:url!, completionHandler: {(data, response, error) in
if error != nil {
print(error)
} else {
do {
let parsedData = try JSONSerialization.jsonObject(with: data!, options: .allowFragments) as! [String:Any]
if let Streams = parsedData["Streams"] as! [AnyObject]? {
for Stream in Streams {
if let post = Stream["post"] as? String {
names.append(post)
}
}
}
} catch let error as NSError {
print(error)
}
print(names)
}
}).resume()
}
What I essentially like to do is put the value of
let post = Stream["post"] as? String
inside the TableView instead of the Groceries array . As I stated before the value is coming back from the Json, I just have not found any way that I could put that value inside the TableView any help would be great . I am using swift 3.0 .
Add reloading data code
DispatchQueue.main.async {
StreamsTableView.reloadData()
}
just after your for loop
for Stream in Streams { ...
if let Streams = parsedData["Streams"] as! [AnyObject]? {
for Stream in Streams {
if let post = Stream["post"] as? String {
names.append(post)
}
}
}
StreamsTableView.reloadData()
After loop done
StreamsTableView.reloadData()
update:
mycell.textLabel?.text = groceries[indexPath.row]
to
mycell.textLabel?.text = names[indexPath.row]

The images from the webservice are refreshed in the UITableview

I was trying to load images from a web service to my tableview. But the images are refreshed and same image rotate in every cell. I think the problem is due to not implementing caching in proper way. I tried lot of stuff. But no luck..
import UIKit
class NewsTableViewController: UITableViewController {
var SLAFImages = [String]()
var SLAFHeading = [String]()
var SLAFText = [String]()
var SLAFCount = [Int]()
#IBOutlet weak var btnAction: UIBarButtonItem!
override func viewDidLoad() {
super.viewDidLoad()
let reposURL = NSURL(string: "https://www.helitours.lk/test.html")
// 2
do{
if let JSONData = NSData(contentsOfURL: reposURL!) {
// 3
if let json = try NSJSONSerialization.JSONObjectWithData(JSONData, options: NSJSONReadingOptions.MutableContainers) as? NSDictionary {
// 4
if let reposArray = json["items"] as? [NSDictionary] {
// 5
for item in reposArray {
if let name = item.valueForKey("title") {
SLAFHeading.append(name as! String)
}
if let Imgname = item.valueForKey("main_image") {
let urlimg=("http://airforce.lk/"+(Imgname as! String)) as String;
SLAFImages.append(urlimg)
}
}
}
}
}
}catch{}
let infoImage = UIImage(named: "crest png.png")
let imgWidth = infoImage?.size.width
let imgHeight = infoImage?.size.height
let button:UIButton = UIButton(frame: CGRect(x: 0,y: 0,width: imgWidth!, height: imgHeight!))
button.setBackgroundImage(infoImage, forState: .Normal)
//button.addTarget(self, action: Selector("openInfo"), forControlEvents: UIControlEvents.TouchUpInside)
SLAFCount=[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20]
SLAFText = ["While they like to munch on underwater plants, you might ",
"While they like to munch on underwater plants, you might ",
"While they like to munch on underwater plants, you might "]
//SLAFImages = ["Img1.jpg","Img2.jpg","Img3.jpg"]
// Uncomment the following line to preserve selection between presentations
// self.clearsSelectionOnViewWillAppear = false
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// MARK: - Table view data source
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of rows
return SLAFHeading.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell =
self.tableView.dequeueReusableCellWithIdentifier(
"NewsTableCell", forIndexPath: indexPath)
as! NewsTableViewCell
let row = indexPath.row
cell.NewsHeading.font =
UIFont.preferredFontForTextStyle(UIFontTextStyleHeadline)
cell.NewsHeading.text = SLAFHeading[row]
if let url = NSURL(string: SLAFImages[row]) {
let task = NSURLSession.sharedSession().dataTaskWithURL(url) { (data, NSHTTPURLResponse, error) -> Void in
if error != nil {
print("thers an error in the log")
} else {
dispatch_async(dispatch_get_main_queue()) {
cell.NewsImage.image = UIImage(data: data!)
}
}
}
task.resume()
}
cell.NewsCount.text=String(SLAFCount[row])
cell.layoutIfNeeded()
return cell
}
Download image in Table view cell
Better way
this code will not increase more memory use of app.
//Initialize your catch variable
var cache = NSCache()
// MARK: - Private Image Download Block here
private func downloadPhoto(url: NSURL, completion: (url: NSURL, image: UIImage) -> Void) {
dispatch_async(downloadQueue, { () -> Void in
if let data = NSData(contentsOfURL: url) {
if let image = UIImage(data: data) {
dispatch_async(dispatch_get_main_queue(), { () -> Void in
cache.setObject(image, forKey: url)
completion(url: url, image: image)
})
}
}
})
}
//Call this Function from Table view
//Step 1
// Checking here Image inside Catch
let imageurl = NSURL(string:”Image URL here”)
let myimage = cache.objectForKey(imageurl!) as? UIImage
//Step 2
// Downloading here Promotion image
if myimage == nil {
downloadPhoto(imageurl!, completion: { (url, image) -> Void in
let indexPath_ = self.tblView.indexPathForCell(cell)
if indexPath.isEqual(indexPath_) {
cell.myImageView.image = image
}
})
}
I hope this will help you.

Resources