将图像从URL下载到图像阵列,然后逐个显示每个图像

时间:2016-04-29 22:39:30

标签: ios swift

所以这就是我想要做的事情:

  
      
  1. Parse.comPFFile查询数组 - 存储在数组中的字符串为.png链接)
  2.   
  3. 使用Haneke将图像从阵列下载到图像数组
  4.   
  5. 将第一张图片设为UIImageView
  6.   
  7. 点击UIImageView时,我想更改为下一张图片。
  8.   

问题是,在我点按UIImageView之前图片不会显示,当我点按以尝试更改图片时,同样的UII图像会一直显示。

这是我的ViewController代码:

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[0].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(){

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

我一直在努力解决这个问题很多时间 - 但我无法弄清楚什么是错的。有人可以帮助我吗?

0 个答案:

没有答案