Kwnstantinos Natsios Kwnstantinos Natsios - 4 months ago 91
iOS Question

Append UI Images from array to slideshow swift

I'm trying to make a slideshow with this library

https://github.com/zvonicek/ImageSlideshow

So i'm making a query, i bring some images and i append them here

var sliderArray = [UIImage]()
var testimg:UIImage!

let idaki = recipeObj.objectId
let pointer2 = PFObject(outDataWithClassName:"Recipes", objectId: idaki!)
let galquery = PFQuery(className:"sliderRecipes")
galquery.whereKey("recipe", equalTo: pointer2)
galquery.findObjectsInBackgroundWithBlock {
(objects: [PFObject]?, error: NSError?) -> Void in


if error == nil {
// The find succeeded.
print("Successfully retrieved \(objects!.count) scores.")
// Do something with the found objects
if let objects = objects {
for object in objects {
count += 1

let glrimg = object["sliderImage"] as! PFFile
glrimg.getDataInBackgroundWithBlock {
(imageData3: NSData?, error: NSError?) -> Void in
if error == nil {
if let imageData2 = imageData3 {
self.testimg = UIImage(data:imageData2)
print(self.testimg)
print(self.sliderArray)
self.sliderArray.append(UIImage(data:imageData2)!)




}
if count == objects.count { print(self.sliderArray) }
}
}



// print("test")
// print(self.sliderPinakas)
}
}
} else {
// Log details of the failure
print("Error: \(error!) \(error!.userInfo)")
}


And after all this i DONT have an array with images. In the print above it first shows the
empty
array and after that it shows the prints of UI Images that i want to show them like this to a slider

slideshow.setImageInputs([ImageSource(image: UIImage(named: "myImage"))!, ImageSource(image: UIImage(named: "myImage2"))!])


but instead of having to do it with the name,
UIImage(named: "myImage"))!
i would like to do it this way

self.slider.setImageInputs([ImageSource(image: self.testimg)])


for all the images of the array, cause the above line shows only one.
Is there anyone that could help?
Thanks!

Answer

You have pass to self.slider.setImageInputs array of all objects ImageSource [ImageSource(image: self.testimg1), ImageSource(image: self.testimg2), ...] etc., not only one object [ImageSource(image: self.testimg)].

If you add images like:
self.slider.setImageInputs([ImageSource(image: self.testimg)])
self.slider.setImageInputs([ImageSource(image: self.testimg2)])
you'll simply replace first image with second.

Create new array, enumerate all images from sliderArray, and append they to array ImageSource(image: self.testimg1) objects.

Also remember, you have to call this from block glrimg.getDataInBackgroundWithBlock!

add before for

var count = 0

and add inside block

count +=1
if count == objects.count {
    print(self.sliderArray)
}.

That will print array when all objects finished loads (they can loads in chaotic order).