当我尝试遍历以下代码时,self.returnImage(displayedPicture!, currentIteration: i, completion: self.printValue)
函数应该具有currentIteration
i
,它应该等于0,但是当代码运行时,它不等于我。
for (var i = 0; i < self.numberOfImages; i++) {
let displayedPicture = object["productImage\(i)"] as? PFFile
self.returnImage(displayedPicture!, currentIteration: i, completion: self.printValue)
var oldI = -1
print(object.objectId)
print("oldI \(oldI)")
print("i = \(i)")
print("Getting objects")
// self.productImages.append((object["productImage\(i)"] as? PFFile)!)
print("oldI = i")
oldI++
}
这是控制台打印出的内容
这是returnImage
函数。由于它从Parse后端检索数据,因此它好像被调用的函数要迟,这是显而易见的,因为在调用函数之前调用prints
,即使它在代码中相反。
func printValue(result: Bool) {
print("result = \(result)")
}
func returnImage(pictureFile: PFFile, let currentIteration: Int, completion: (result: Bool) -> Void) {
pictureFile.getDataInBackgroundWithBlock({ (imageData, error) -> Void in
if error != nil {
print(error)
completion(result: false)
} else {
if let data = imageData {
print("currentIteration = \(currentIteration)")
//var circleArray = Array<UIImageView>()
//var y : CGFloat = 0.0
//var i = 0
self.imageViewArray.append(UIImageView(frame:CGRectMake(self.screenWidth/2 - 120, self.screenHeight/2 - CGFloat(self.numberOfImages + 80), 120, 120)))
print("Number Of Images Count = \(self.numberOfImages)")
print("ImageViewArray Count = \(self.imageViewArray.count)")
self.imageView = self.imageViewArray[currentIteration] // This is the line where the Array index out of range occurs
self.imageView.image = UIImage(data: data)!
self.imageView.tag = currentIteration
self.view.addSubview(self.imageView)
//self.imageView[i].frame = CGRectMake(self.screenWidth/2 - 120, self.screenHeight/2 - CGFloat(self.numberOfImages + 10), 60, 60)
//self.imageView[i].image = UIImage(data: data)!
self.imageView.contentMode = UIViewContentMode.ScaleAspectFill
self.imageView.layer.cornerRadius = self.imageView.frame.size.width / 2
self.imageView.clipsToBounds = true
}
}
completion(result: true)
})
}
这是完整的代码。数组索引超出范围错误发生在self.imageView = self.imageViewArray[currentIteration]
import UIKit
import Parse
class BusinessDetailsVC: UIViewController, UIScrollViewDelegate {
var businessTitle: String = ""
let scrollView: UIScrollView = UIScrollView()
let screenHeight = UIScreen.mainScreen().bounds.size.height
let screenWidth = UIScreen.mainScreen().bounds.size.width
var numberOfImages: Int = 0
var nameOfImage: [String] = [String]()
var productImages: [PFFile] = [PFFile]()
var downloadedImage: UIImage = UIImage()
var imageViewArray = Array<UIImageView>()
var imageView: UIImageView = UIImageView()
//var displayedPicture: [PFObject] = [PFObject]()
//var imageView = UIImageView()
override func viewDidLoad() {
super.viewDidLoad()
print("On businessDetailsVC")
let query = PFQuery(className: "C_h0usYnpEqL")
print("Number of images \(numberOfImages)")
query.findObjectsInBackgroundWithBlock { (objects, error) -> Void in
if error != nil {
print(error)
} else if objects! == objects! {
print("Number of objects \(objects?.count)")
for object in objects! {
for (var i = 0; i < self.numberOfImages; i++) {
let displayedPicture = object["productImage\(i)"] as? PFFile
self.returnImage(displayedPicture!, currentIteration: i, completion: self.printValue)
var oldI = -1
print(object.objectId)
print("oldI \(oldI)")
print("i = \(i)")
print("Getting objects")
// self.productImages.append((object["productImage\(i)"] as? PFFile)!)
print("oldI = i")
oldI++
}
}
}
}
}
func printValue(result: Bool) {
print("result = \(result)")
}
func returnImage(pictureFile: PFFile, let currentIteration: Int, completion: (result: Bool) -> Void) {
pictureFile.getDataInBackgroundWithBlock({ (imageData, error) -> Void in
if error != nil {
print(error)
completion(result: false)
} else {
if let data = imageData {
print("currentIteration = \(currentIteration)")
//var circleArray = Array<UIImageView>()
//var y : CGFloat = 0.0
//var i = 0
self.imageViewArray.append(UIImageView(frame:CGRectMake(self.screenWidth/2 - 120, self.screenHeight/2 - CGFloat(self.numberOfImages + 80), 120, 120)))
print("Number Of Images Count = \(self.numberOfImages)")
print("ImageViewArray Count = \(self.imageViewArray.count)")
self.imageView = self.imageViewArray[currentIteration] // This is the line where the Array index out of range occurs
self.imageView.image = UIImage(data: data)!
self.imageView.tag = currentIteration
self.view.addSubview(self.imageView)
//self.imageView[i].frame = CGRectMake(self.screenWidth/2 - 120, self.screenHeight/2 - CGFloat(self.numberOfImages + 10), 60, 60)
//self.imageView[i].image = UIImage(data: data)!
self.imageView.contentMode = UIViewContentMode.ScaleAspectFill
self.imageView.layer.cornerRadius = self.imageView.frame.size.width / 2
self.imageView.clipsToBounds = true
}
}
completion(result: true)
})
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
答案 0 :(得分:0)
根据您当前的逻辑,尝试使用以下代码添加ImageView,
self.imageViewArray.append(UIImageView(frame:CGRectZero))
因为如果发生错误,则下一次迭代的数组中的图像数量会减少。 例如如果在迭代3中发生错误,则对于下一次迭代4,如果迭代4成功,则只有3个图像。