gpt4 book ai didi

ios - 从 URL 下载图像并作为图像添加到数组

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:00:50 25 4
gpt4 key购买 nike

这就是我想要做的:

  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.

问题是在我点击 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

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

最佳答案

我认为问题在于 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()
}

关于ios - 从 URL 下载图像并作为图像添加到数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36927056/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com