iOS Keep url array order when saving images into an array - ios

I'm trying to do the following.
I have an array that contains several URLs for different images. These images will be displayed into a scrollview and I want to keep this order to be able to delete specific images at a specific index.
Right now I'm using a for loop, first I load the array with NSNull objects and afterward I run the loop replacing all the objects with images. This doesn't seem to be really efficient, specially when the app loads from the background.
This is my code:
func downloadImagesfromUrlArray(){
for var i=0;i < graphsURLlist.count; i++ {
if let data = NSData(contentsOfURL: graphsURLlist[i]){
let image = UIImage(data: data)
pageImages.replaceObjectAtIndex(i, withObject: image!)
//self.scaleImagesandScrollView()
}else{
NSLog("%#", i)
var alert = myAlertController(title: nil, message: "Error trying to update the charts. Try again later \(i)",
preferredStyle: UIAlertControllerStyle.Alert)
alert.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.Default, handler: nil))
self.presentViewController(alert, animated: true, completion: nil)
}
//Enter queue for every image to download
}
dispatch_group_leave(self.group)
}
Is there a better approach to do this?
Thanks

You better want to download images asynchronously and cache them. Before the images are downloaded it's better to display spinner, and then replace it with an image.
Here are a few related methods from my latest app:
func downloadAndCacheImg(url: String?, cell: AppCell, tableView: UITableView, indexPath: NSIndexPath) {
if url == nil { return }
let charactersToRemove = NSCharacterSet.alphanumericCharacterSet().invertedSet
let cacheFilename = "".join(url!.componentsSeparatedByCharactersInSet(charactersToRemove))
if let fullFilename = prefixFilenameWithDirectory(cacheFilename), img = UIImage(named: fullFilename) {
cell.setAppImage(img)
return
}
if let nsUrl = NSURL(string: url!) {
NSURLConnection.sendAsynchronousRequest(NSURLRequest(URL: nsUrl), queue: NSOperationQueue.mainQueue(), completionHandler: { (response, data, error) -> Void in
if let data = data, img = UIImage(data: data) {
if let fullFilename = self.prefixFilenameWithDirectory(cacheFilename) {
data.writeToFile(fullFilename, atomically: true)
}
if let cell = tableView.cellForRowAtIndexPath(indexPath) as? AppCell {
cell.setAppImage(img)
}
}
})
}
}
func prefixFilenameWithDirectory(filename: String) -> String? {
var dir = NSFileManager.defaultManager().URLForDirectory(.CachesDirectory, inDomain: .UserDomainMask, appropriateForURL: nil, create: true, error: nil)
dir = dir?.URLByAppendingPathComponent(filename)
return dir?.path
}

Related

Failling to reorder tableView cell images

For study purposes, I'm creating a app to show a list of some star wars ships. It fetches my json (locally) for the ship objects (it has 4 ships for this example).
It's using a custom cell for the table view.
The table populates without problems, if I already have the images downloaded (in user documents) or not.
My starshipData array is populated by my DataManager class by delegate.
I removed some code to make the class smaller, I can show everything if needed.
Ok, so the problem happens (very rarely) when I press the sorting button.
The way I'm doing it is after recovering or downloading the image, I update the image field in starshipData array.
Here is my sorting method, pretty basic.
#objc private func sortByCost(sender: UIBarButtonItem) {
starshipData.sort { $0.costInCredits < $1.costInCredits }
starshipTableView.reloadData()
}
Here are the implementations of the tableView.
First I use the cellForRowAt method to populate the fast/light data.
// MARK: -> cellForRowAt
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "StarshipCell", for: indexPath) as! StarshipCell
let starship = starshipData[indexPath.row]
// update cell properties
cell.starshipNameLabel.text = starship.name
cell.starshipManufacturerLabel.text = starship.manufacturer
cell.starshipCostLabel.text = currencyFormatter(value: starship.costInCredits)
// only populate the image if the array has one (the first time the table is populated,
// the array doesn't have an image, it'll need to download or fetch it in user documents)
if starship.image != nil {
cell.starshipImgView.image = starship.image
}
// adds right arrow indicator on the cell
cell.accessoryType = .disclosureIndicator
return cell
}
Here I use the willDisplay method to download or fetch the images, basically the heavier data.
// MARK: -> willDisplay
override func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
// update cell image
let cell = cell as! StarshipCell
let imageUrl = starshipData[indexPath.row].imageUrl
let starshipName = starshipData[indexPath.row].name
let index = indexPath.row
// if there isn't any image on the cell, proceed to manage the image
if cell.starshipImgView.image == nil {
// only instantiate spinner on imageView position if no images are set
let spinner = UIActivityIndicatorView(style: .medium)
startSpinner(spinner: spinner, cell: cell)
// manage the image
imageManager(starshipName: starshipName, imageUrl: imageUrl, spinner: spinner, cell: cell, index: index) { (image) in
self.addImageToCell(cell: cell, spinner: spinner, image: image)
}
}
}
Here is where I think the problem is as my knowledge in swift and background threads are still in development.
I found out with print logs that the times the cell doesn't show the correct image is because the array does not have the image for that index, so the cell shows the image from the last time the table was populated/loaded.
I wonder if it's because the background threads didn't have enough time to update the starshipArray with the fetched/downloaded image before the user pushing the sort button.
The thing is, if the table was populated correctly the first time, when the sort button is pushed, the starshipData array should already have all images, as you can see in the imageManager method, after the image is unwrappedFromDocuments, I call updateArrayImage to update the image.
Maybe it's the amount of dispatchesQueues being used? Are the completion handler and dispatchQueues used correctly?
private func imageManager(starshipName: String, imageUrl: URL?, spinner: UIActivityIndicatorView, cell: StarshipCell, index: Int, completion: #escaping (UIImage) -> Void) {
// if json has a string on image_url value
if let unwrappedImageUrl = imageUrl {
// open a background thread to prevent ui freeze
DispatchQueue.global().async {
// tries to retrieve the image from documents folder
let imageFromDocuments = self.retrieveImage(imageName: starshipName)
// if image was retrieved from folder, upload it
if let unwrappedImageFromDocuments = imageFromDocuments {
// TO FORCE THE PROBLEM DESCRIBED, PREVENT ONE SHIP TO HAVE IT'S IMAGE UPDATED
// if (starshipName != "Star Destroyer") {
self.updateArrayImage(index: index, image: unwrappedImageFromDocuments)
// }
completion(unwrappedImageFromDocuments)
}
// if image wasn't retrieved or doesn't exists, try to download from the internet
else {
var image: UIImage?
self.downloadManager(imageUrl: unwrappedImageUrl) { data in
// if download was successful
if let unwrappedData = data {
// convert image data to image
image = UIImage(data: unwrappedData)
if let unwrappedImage = image {
self.updateArrayImage(index: index, image: unwrappedImage)
// save images locally on user documents folder so it can be used whenever it's needed
self.storeImage(image: unwrappedImage, imageName: starshipName)
completion(unwrappedImage)
}
}
// if download was not successful
else {
self.addImageNotFound(spinner: spinner, cell: cell)
}
}
}
}
}
// if json has null on image_url value
else {
addImageNotFound(spinner: spinner, cell: cell)
}
}
Here are some of the helper methods I use on imageManager, if necessary.
// MARK: - Helper Methods
private func updateArrayImage(index: Int, image: UIImage) {
// save image in the array so it can be used when cells are sorted
self.starshipData[index].image = image
}
private func downloadManager(imageUrl: URL, completion: #escaping (Data?) -> Void) {
let session: URLSession = {
let configuration = URLSessionConfiguration.default
configuration.timeoutIntervalForRequest = 5
return URLSession(configuration: configuration, delegate: nil, delegateQueue: nil)
}()
var dataTask: URLSessionDataTask?
dataTask?.cancel()
dataTask = session.dataTask(with: imageUrl) { [weak self] data, response, error in
defer {
dataTask = nil
}
if let error = error {
// use error if necessary
DispatchQueue.main.async {
completion(nil)
}
}
else if let response = response as? HTTPURLResponse,
response.statusCode != 200 {
DispatchQueue.main.async {
completion(nil)
}
}
else if let data = data,
let response = response as? HTTPURLResponse,
response.statusCode == 200 { // Ok response
DispatchQueue.main.async {
completion(data)
}
}
}
dataTask?.resume()
}
private func addImageNotFound(spinner: UIActivityIndicatorView, cell: StarshipCell) {
spinner.stopAnimating()
cell.starshipImgView.image = #imageLiteral(resourceName: "ImageNotFound")
}
private func addImageToCell(cell: StarshipCell, spinner: UIActivityIndicatorView, image: UIImage) {
DispatchQueue.main.async {
spinner.stopAnimating()
cell.starshipImgView.image = image
}
}
private func imagePath(imageName: String) -> URL? {
let fileManager = FileManager.default
// path to save the images on documents directory
guard let documentPath = fileManager.urls(for: .documentDirectory,
in: FileManager.SearchPathDomainMask.userDomainMask).first else { return nil }
let appendedDocumentPath = documentPath.appendingPathComponent(imageName)
return appendedDocumentPath
}
private func retrieveImage(imageName: String) -> UIImage? {
if let imagePath = self.imagePath(imageName: imageName),
let imageData = FileManager.default.contents(atPath: imagePath.path),
let image = UIImage(data: imageData) {
return image
}
return nil
}
private func storeImage(image: UIImage, imageName: String) {
if let jpgRepresentation = image.jpegData(compressionQuality: 1) {
if let imagePath = self.imagePath(imageName: imageName) {
do {
try jpgRepresentation.write(to: imagePath,
options: .atomic)
} catch let err {
}
}
}
}
private func startSpinner(spinner: UIActivityIndicatorView, cell: StarshipCell) {
spinner.center = cell.starshipImgView.center
cell.starshipContentView.addSubview(spinner)
spinner.startAnimating()
}
}
To sum all up, here is the unordered table, when you open the app: unordered
The expected result (happens majority of time), after pushing the sort button: ordered
The wrong result (rarely happens), after pushing the sort button: error
I'll gladly add more info if needed, ty!
First, consider move the cell configuration for the UITableViewCell class. something like this:
class StarshipCell {
private var starshipNameLabel = UILabel()
private var starshipImgView = UIImageView()
func configure(with model: Starship) {
starshipNameLabel.text = model.name
starshipImgView.downloadedFrom(link: model.imageUrl)
}
}
Call the configure(with: Starship) method in tableView(_:cellForRowAt:).
The method downloadedFrom(link: ) called inside the configure(with: Starship) is provide by following extension
extension UIImageView {
func downloadedFrom(url: URL, contentMode mode: UIView.ContentMode = .scaleAspectFit) {
contentMode = mode
URLSession.shared.dataTask(with: url) { data, response, error in
guard let httpURLResponse = response as? HTTPURLResponse, httpURLResponse.statusCode == 200,
let mimeType = response?.mimeType, mimeType.hasPrefix("image"),
let data = data, error == nil,
let image = UIImage(data: data)
else { return }
DispatchQueue.main.async() {
self.image = image
}
}.resume()
}
func downloadedFrom(link: String?, contentMode mode: UIView.ContentMode = .scaleAspectFit) {
if let link = link {
guard let url = URL(string: link) else { return }
downloadedFrom(url: url, contentMode: mode)
}
}
}

UIImagePickerController not picking the image

I am trying to make a TableView App that allows adding images and names to it using the ImagePickerController and changing its name via an AlertController TextField
The problem is the ImagePicker is not picking up the Image and no error is shown on the debugging console.
When I click the Image to pick it, nothing happens.
class ViewController: UITableViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
var photos = [Photo]()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
title = "Snapshots"
navigationController?.navigationBar.prefersLargeTitles = true
navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: .add, target: self, action: #selector(addPhoto))
let defaults = UserDefaults.standard
if let savedPhotos = defaults.object(forKey: "photos") as? Data {
let jsonDecoder = JSONDecoder()
do {
photos = try jsonDecoder.decode([Photo].self, from: savedPhotos)
} catch {
print("Failed to load photos.")
}
}
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return photos.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
guard let cell = tableView.dequeueReusableCell(withIdentifier: "Photo", for: indexPath) as? PhotoCell else {
fatalError("Could not load the Photo Cell")
}
let photo = photos[indexPath.row]
cell.textLabel?.text = photo.name
let path = getDocumentsDirectory().appendingPathComponent(photo.image)
cell.imageView?.image = UIImage(contentsOfFile: path.path)
return cell
}
#objc func addPhoto() {
let picker = UIImagePickerController()
picker.isEditing = true
picker.delegate = self
present(picker, animated: true)
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
guard let image = info[.editedImage] as? UIImage else { return }
let imageName = UUID().uuidString
let imagePath = getDocumentsDirectory().appendingPathComponent(imageName)
if let jpegData = image.jpegData(compressionQuality: 0.8) {
try? jpegData.write(to: imagePath)
}
let photo = Photo(name: "New Image", image: imageName)
photos.append(photo)
save()
tableView.reloadData()
dismiss(animated: true)
}
func getDocumentsDirectory() -> URL {
let paths = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)
return paths[0]
}
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let photo = photos[indexPath.row]
let ac = UIAlertController(title: "What you want to do with Image?", message: nil, preferredStyle: .alert)
ac.addTextField()
ac.addAction(UIAlertAction(title: "Rename", style: .default) {
[weak self, weak ac] _ in
guard let newName = ac?.textFields?[0].text else { return }
photo.name = newName
self?.save()
self?.tableView.reloadData()
})
ac.addAction(UIAlertAction(title: "Delete", style: .destructive) {
[weak self] _ in
self?.photos.remove(at: indexPath.row)
self?.tableView.reloadData()
})
ac.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))
ac.addAction(UIAlertAction(title: "Open", style: .default) {
[weak self] _ in
if let vc = self?.storyboard?.instantiateViewController(withIdentifier: "PhotoView") as? DetailViewController {
vc.selectedImage = photo.name
self?.navigationController?.pushViewController(vc, animated: true)
}
})
present(ac, animated: true)
}
func save() {
let jsonEncoder = JSONEncoder()
if let savedData = try? jsonEncoder.encode(photos) {
let defaults = UserDefaults.standard
defaults.set(savedData, forKey: "photos")
} else {
print("Failed to save photos.")
}
}
}
I actually took your code and condensed it down quite a bit to test locally and found one possible issue here:
guard let image = info[.editedImage] as? UIImage else { return }
If your user has not edited a photo, then the return kicks in and nothing further happens.
However, if I change it to .originalImage like this, then simply selecting an image will allow it to proceed:
guard let image = info[.originalImage] as? UIImage else { return }
With that said, you may want to consider a switch statement, or some if/else to facilitate different types of images based on whatever suits your app.
Have you added Photo Library Usage permission in info.plist file? If you havn't then add "Privacy - Photo Library Usage Description " in info.plist.

How to download image from asynchronously in swift

Hi I am developing a app using swift2.2 am getting a data from a server and am passing it to the collection view.am getting more images from the server i cant pass it very quickly it consumes so much of time so i need to to pass that quickly as much as possible.
code in my view controller:
func jsonParsingFromURL() {
self.view.makeToastActivity(.Center)
if Reachability.isConnectedToNetwork() == true {
Alamofire.request(.POST, "http://something.com=\(appDelCityname)&fromLimit=0&toLimit=20", parameters: nil, encoding: .URL, headers: nil).response {
(req, res, data, error) - > Void in
let dataString = NSString(data: data!, encoding: NSUTF8StringEncoding)
print(dataString)
let json = JSON(data: data!)
let jsonData = json["ad_count"].int
if jsonData == 0 {
let alert = UIAlertController(title: "sorry!", message: "No Ads availabe", preferredStyle: UIAlertControllerStyle.Alert)
alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: nil))
self.presentViewController(alert, animated: true, completion: nil)
} else {
self.startParsing(data!)
}
}
} else {
let alert = UIAlertController(title: "No Internet Connection", message: "make sure your device is connected to the internet", preferredStyle: UIAlertControllerStyle.Alert)
alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: nil))
self.presentViewController(alert, animated: true, completion: nil)
}
}
func startParsing(data: NSData) {
let json = JSON(data: data)
let mainArr = json["subcategory"].array!
for item in mainArr {
self.fetchImage = item["images"].stringValue
self.fetchPrice = item["originalprice"].stringValue
self.fetchTitle = item["adtitle"].stringValue
self.fetchCityname = item["districtname"].stringValue
let takefirstImg = self.fetchImage.componentsSeparatedByString(",")
let firstimgofStr = takefirstImg[0]
dispatch_async(dispatch_get_main_queue(), {
() - > Void in
let URL_API_HOST2: String = "https://www.something.com"
let url = NSURL(string: URL_API_HOST2 + firstimgofStr)
let data = NSData(contentsOfURL: url!)
let imageyy = UIImage(data: data!)
self.priceArr.append(self.fetchPrice)
self.cityArr.append(self.fetchCityname)
self.adtitleArr.append(self.fetchTitle)
print("something = \(firstimgofStr)")
print("check image = \(self.fetchTitle)")
self.imageDict.append(imageyy!)
self.view.hideToastActivity()
})
self.refresh()
}
}
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) - > Int {
return imageDict.count
}
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) - > UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier("FilterCell", forIndexPath: indexPath) as!SecondTableViewCell
cell.FifTitle.text = adtitleArr[indexPath.item]
cell.FifAddress.text = cityArr[indexPath.item]
cell.Fifprice.text = priceArr[indexPath.item]
dispatch_async(dispatch_get_main_queue(), {
cell.Fifimage.image = self.imageDict[indexPath.item]
})
// cell.Fifimage.sd_setImageWithURL(NSURL(string: URL_API_HOST2 + (firstImage as String)))
return cell
}
func collectionView(collectionView: UICollectionView, layout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) - > CGSize {
return imageDict[indexPath.item].size
}
I am having the library of SDWebImage but which will allow only url type but i have to use UIImage value to calculate the size of image to get Pinterest style view.according to my method it works well but when i get more value from the server it makes very late to display so help me to work with this process am struggling from the morning.

How to use closures to return a JSON value from a NSURLSession

I am trying to return a result from a JSON object but unable to do so. I am new to Swift so kindly explain me how to do so. In the below code I want to return json_level_number in the return of function fetchNumberOfSections () where i have hard coded as 5 right now return 5. If i declare a variable json_level_number just above the reachability code it sort of solves the problem but then it is returning '0' for the first time. The API returns 2 each time.
Code as below:
func fetchNumberOfSections () -> Int {
if Reachability.isConnectedToNetwork() == true {
// Below code to fetch number of sections
var urlAsString = "http://themostplayed.com/rest/fetch_challenge_sections.php"
urlAsString = urlAsString+"?apiKey="+apiKey
print (urlAsString)
let url = NSURL(string: urlAsString)!
let urlSession = NSURLSession.sharedSession()
let jsonQuery = urlSession.dataTaskWithURL(url, completionHandler: { data, response, error -> Void in
if (error != nil) {
print(error!.localizedDescription)
}
do {
let jsonResult = (try NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions.MutableContainers)) as! NSDictionary
let json_level_number: String! = jsonResult["level_number"] as! String
//
dispatch_async(dispatch_get_main_queue(), {
// self.dateLabel.text = jsonDate
// self.timeLabel.text = jsonTime
print(json_level_number)
// self.activityIndicatorStop()
})
}
catch let errorJSON {
print (errorJSON)
// alert box code below
let alert = UIAlertController(title: "JSON Error!", message:"Error processing JSON.", preferredStyle: .Alert)
let action = UIAlertAction(title: "OK", style: .Default) { _ in
// Put here any code that you would like to execute when
self.dismissViewControllerAnimated(true, completion: {})
}
alert.addAction(action)
self.presentViewController(alert, animated: true, completion: nil)
// alert box code end
}
})
jsonQuery.resume()
// End
}
else {
print("Internet connection FAILED")
self.performSegueWithIdentifier("nointernet", sender: nil)
}
return 5
}

Tab with UITableView takes long to load

I have a tab that contains a UITableView. The UITableView loads JSON from a server. Here are some lines in my viewDidLoad():
// Register the UITableViewCell class with the tableView
self.tableView?.registerClass(UITableViewCell.self, forCellReuseIdentifier: self.cellIdentifier)
var tblView = UIView(frame: CGRectZero)
tableView.tableFooterView = tblView
tableView.backgroundColor = UIColor.clearColor()
startConnection()
Here is my startConnection():
func startConnection() {
let url = NSURL(string: "some correct URL")
var request = NSURLRequest(URL: url!)
var data = NSURLConnection.sendSynchronousRequest(request, returningResponse: nil, error: nil)
if data != nil {
var json = JSON(data: data!)
if let jsonArray = json.arrayValue {
for jsonDict in jsonArray {
var pageName: String? = jsonDict["title"].stringValue
//some code
}
}
activityIndicator.stopAnimating()
} else {
println("No data")
activityIndicator.stopAnimating()
var alert = UIAlertController(title: "No data", message: "No data received", preferredStyle: UIAlertControllerStyle.Alert)
let cancelAction = UIAlertAction(title: "Cancel", style: .Cancel) { (action) in
}
alert.addAction(cancelAction)
let OKAction = UIAlertAction(title: "Retry", style: .Default) { (action) in
self.startConnection()
}
alert.addAction(OKAction)
self.presentViewController(alert, animated: true, completion: nil)
}
}
After the first load, the tab will show upon clicking. I am thinking if it is the NSURLConnection.sendSynchronousRequest causing the lag. Any advice and suggestion? I don't really know how to use sendAsynchronousRequest either =/ Please help. Thank you! =D
Although there are several ways to get data from a website, the important thing is that you execute your UI updates on the main thread using dispatch_async(dispatch_get_main_queue()) {}. This is because URL tasks such as NSURLSession.dataTaskWithURL or NSURLConnection.sendAsynchronousRequest() {} execute on a background thread so if you don't explicitly update UI on the main thread you will often experience a lag. Here's what a simple request looks like:
func fetchJSON(sender: AnyObject?) {
let session = NSURLSession.sharedSession()
let url: NSURL! = NSURL(string: "www.someurl.com")
session.dataTaskWithURL(url) { (data, response, error) in
var rawJSON: AnyObject? = NSJSONSerialization.JSONObjectWithData(data, options: .allZeros, error: nil)
if let result = rawJSON as? [[String: AnyObject]] {
dispatch_async(dispatch_get_main_queue()) {
// Update your UI here ie. tableView.reloadData()
}
}
}.resume()
}
You can do it like this:
let url:NSURL = NSURL(string:"some url")
let request:NSURLRequest = NSURLRequest(URL:url)
let queue:NSOperationQueue = NSOperationQueue()
NSURLConnection.sendAsynchronousRequest(request, queue: queue, completionHandler:{ (response: NSURLResponse!, data: NSData!, error: NSError!) -> Void in
/* Your code */
})

Resources