Roduck Nickes Roduck Nickes - 7 months ago 84
Swift Question

Download images from URL and add to array as image

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

Answer

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()
    }