从URL下载图像并将其添加到图像

时间:2016-04-28 23:19:52

标签: ios swift parse-platform uiimageview

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

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

问题是,在我点按UIImageView之前图像不会显示,而当我点按以尝试更改图像时,相同的UIImage始终显示。

这是我的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[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()
            }
        }
    }
}

我现在一直在努力解决这个问题几个小时 - 但仍然无法解决。

点击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

编辑:很奇怪......当我在print(imageArray.description)函数中运行tapGesture时,我得到了这个输出:http://pastebin.com/H0u97pz5

1 个答案:

答案 0 :(得分:0)

我认为问题在于imageToArray是一个常量。尝试:

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