I'm trying to do the following.
I have an array that contains several URLs for different images. These images will be displayed into a scrollview and I want to keep this order to be able to delete specific images at a specific index.
Right now I'm using a for loop, first I load the array with NSNull
objects and afterward I run the loop replacing all the objects with images. This doesn't seem to be really efficient, specially when the app loads from the background.
This is my code:
func downloadImagesfromUrlArray(){
for var i=0;i < graphsURLlist.count; i++ {
if let data = NSData(contentsOfURL: graphsURLlist[i]){
let image = UIImage(data: data)
pageImages.replaceObjectAtIndex(i, withObject: image!)
//self.scaleImagesandScrollView()
}else{
NSLog("%@", i)
var alert = myAlertController(title: nil, message: "Error trying to update the charts. Try again later \(i)",
preferredStyle: UIAlertControllerStyle.Alert)
alert.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.Default, handler: nil))
self.presentViewController(alert, animated: true, completion: nil)
}
//Enter queue for every image to download
}
dispatch_group_leave(self.group)
}
Is there a better approach to do this? Thanks
You better want to download images asynchronously and cache them. Before the images are downloaded it's better to display spinner, and then replace it with an image.
Here are a few related methods from my latest app: