Download images from URL and add to array as image

Roduck Nickes

So this is what I am trying to do:

  1. Query from Parse.com (PFFile to array - strings stored in array as .png link)
  2. Download the images from array using Haneke to a array of images
  3. Set the first image to a UIImageView.
  4. When tapping the UIImageView, I want to change to the next image.

The problem is that the image doesn't show up until i tap the UIImageView, and when I tap to try change the image, the same UIImage is showing all the time.

This is what my ViewController code looks like:

import UIKit
import Parse

class ViewController: UIViewController {

    var userFile = [PFFile]()
    var createdAt = [NSDate]()
    var objID = [String]()

    var countInt = 0

    @IBOutlet var imageView: UIImageView!

    var imageArray: [UIImageView] = []
    let imageToArray = UIImageView()

    override func viewDidLoad() {
        super.viewDidLoad()

        imageToArray.frame.size = CGSizeMake(imageView.frame.size.width, imageView.frame.size.height)

        queryStory()

        let tap = UITapGestureRecognizer(target: self, action: #selector(handleTap))
        imageView.addGestureRecognizer(tap)
        imageView.userInteractionEnabled = true
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }

    func downloadImages() {
        if (countInt <= userFile.count - 1){
            imageToArray.hnk_setImageFromURL(NSURL(string: userFile[countInt].url!)!)
            countInt = countInt + 1
            imageArray.insert(imageToArray, atIndex: 0)
            print("Image downloaded. Current count: \(imageArray.count)")
            self.downloadImages()
        }
        else{
            print("no more items")
            countInt = 0
            setImage()
        }

    }

    func setImage() {
        imageView.image = imageArray[countInt].image
        countInt = countInt + 1
        print("setImage set")
    }

    func handleTap(gestureRecognizer: UIGestureRecognizer)
    {
        print("tapped")

        if (countInt <= imageArray.count - 1){
            imageView.image = nil
            print("set new image")
            imageView.image = imageArray[countInt].image
            countInt = countInt + 1
        }
        else{
            print("no more items")
        }
    }

    func queryStory(){
        self.userFile.removeAll()
        self.objID.removeAll()
        self.createdAt.removeAll()

        let query = PFQuery(className: "myClass")
        query.orderByDescending("createdAt")

        query.findObjectsInBackgroundWithBlock { (posts: [PFObject]?, error: NSError?) -> Void in
            if (error == nil){
                // Success fetching objects

                print("Post count:", posts!.count)

                for post in posts! {

                    if let imagefile = post["userFile"] as? PFFile {
                        self.userFile.append(post["userFile"] as! PFFile)
                        self.objID.append(post.objectId!)
                        self.createdAt.append(post.createdAt!)
                    }
                }

                dispatch_async(dispatch_get_main_queue()) {
                    print("Done")
                    self.downloadImages()
                }

                print("Uploaded files count: ", self.userFile.count)
            }
            else{
                print(error)

                let alert = UIAlertView()
                alert.title = "Error"
                alert.message = error?.localizedDescription
                alert.addButtonWithTitle("OK")
                alert.show()
            }
        }
    }
}

I've been messing around trying to fix this for a few hours now - but still can't.

This is the full output after I tap the UIImage:

Post count: 8
Uploaded files count:  8
Post count: 8
Uploaded files count:  8
Done
Image downloaded. Current count: 1
Image downloaded. Current count: 2
Image downloaded. Current count: 3
Image downloaded. Current count: 4
Image downloaded. Current count: 5
Image downloaded. Current count: 6
Image downloaded. Current count: 7
Image downloaded. Current count: 8
no more items
setImage set
tapped
set new image

EDIT: Weird... When I run print(imageArray.description) in the tapGesture function, I get this output: http://pastebin.com/H0u97pz5

Jefinthejam

I think the problem is that imageToArray is a constant. Try:

func downloadImages() {
    if (countInt <= userFile.count - 1){
        var imageToInsert = UIImageView()
        imageToInsert.hnk_setImageFromURL(NSURL(string: userFile[countInt].url!)!)
        countInt = countInt + 1
        imageArray.insert(imageToInsert, atIndex: 0)
        print("Image downloaded. Current count: \(imageArray.count)")
        self.downloadImages()
    }
    else{
        print("no more items")
        countInt = 0
        setImage()
    }

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Download images from url in order

From Java

How to download image from url

From Dev

download and store image from url

From Dev

Download image from URL synchronously

From Dev

Download image from URL synchronously

From Dev

Android Download Image from URL

From Dev

Download Image from an url in Android

From Dev

Image ByteArray from file not matching same Image Byte Array download from url

From Dev

Download images from a HTTPS URL in Java

From Dev

Powershell - Download Image from an image url

From Dev

Powershell - Download Image from an image url

From Dev

Download images from specific array of urls with php

From Dev

How to download images from array to tableviewcontroller in swift?

From Dev

How to download images from array to tableviewcontroller in swift?

From Dev

Download image from URL in loop for collection view

From Dev

how to download and display an image from an URL in R?

From Dev

Elixir - Download a File (Image) from a URL

From Dev

node.js download image from url

From Dev

PHP download jpeg image from url

From Dev

IOS how to download image from URL

From Dev

Download Image From Steam Url PHP

From Dev

Force a maximum time to download image from URL

From Dev

Download .webp image from url with C#

From Dev

Compress image from URL before download?

From Dev

How to download an image from URL in MVC

From Dev

Download image from url and put in buffer

From Dev

Download image from URL and draw it on canvas - Android

From Dev

Download an image from a url, log error

From Dev

download image from JSON url with urllib