Ios 从URL下载图像并作为图像添加到阵列

Ios 从URL下载图像并作为图像添加到阵列,ios,swift,parse-platform,uiimageview,Ios,Swift,Parse Platform,Uiimageview,这就是我想做的: 从Parse.com(PFFile到数组的查询-作为.png链接存储在数组中的字符串) 使用将图像从阵列下载到图像阵列 将第一个图像设置为UIImageView 点击UIImageView时,我想切换到下一个图像 问题是,在我点击UIImageView之前,图像不会显示,当我点击尝试更改图像时,始终显示相同的UIImage。 以下是我的ViewController代码: import UIKit import Parse class ViewController: UIVie

这就是我想做的:

  • Parse.com
    PFFile
    到数组的查询-作为.png链接存储在数组中的字符串)
  • 使用将图像从阵列下载到图像阵列
  • 将第一个图像设置为
    UIImageView
  • 点击
    UIImageView
    时,我想切换到下一个图像
  • 问题是,在我点击
    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()
                }
            }
        }
    }
    

    编辑:奇怪。。。当我在
    点击手势
    函数中运行
    print(imageArray.description)
    时,我得到以下输出:

    我认为问题在于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()
        }
    
    func下载图像(){
    
    if(countInt现在看一看,添加了2个输出。好的,那么else分支执行得很好。如果你再次点击,你会在控制台中得到另一个“点击”吗?(我很好奇,因为在你提供的额外输出中,
    userInteractionEnabled=NO
    )是的,当我再次点击时,我会得到另一个“点击”在控制台中。但是看看我添加的新输出:-这是存储图像的数组描述。它们看起来都一样?哦,你说得对,我没有注意到。这很有趣-看起来所有图像都一样。你能将
    handleTap
    中的print语句更改为
    print(“设置新图像:\(countInt)”)
    要查看每次点击时是否正确计数?请尝试并正确计数:
    后计数:8个上载文件计数:8个后计数:8个下载完成的图像。当前计数:下载1个图像。当前计数:下载2个图像。当前计数:下载3个图像。当前计数:下载4个图像。当前计数:下载5个图像。Curr耳鼻喉科计数:下载6张图像。当前计数:下载7张图像。当前计数:8张下载图像:无更多项目设置图像集点击设置新图像:1张点击设置新图像:2张点击设置新图像:3张点击设置新图像:4张点击设置新图像:5张点击设置新图像:6
    ,但图像完全不变。
    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()
        }