Reputation: 3804
I am relatively new to iOS and would appreciate any help figuring out how to create an array of images when using an extension to help manage downloads and caching. I've been going around in circles with this for a couple hours so thought it was time to tap into the brain trust.
I created an extension of UIImageView in order to download and cache images like this:
let imageCache = NSCache()
extension UIImageView {
func loadImageUsingCacheWithURLString(url: NSURL) {
self.image = nil
// First check if there is an image in the cache
if let cachedImage = imageCache.objectForKey(url) as? UIImage {
self.image = cachedImage
return
}
else {
// Otherwise download image using the url location in Google Firebase
NSURLSession.sharedSession().dataTaskWithURL(url, completionHandler: { (data, response, error) in
if error != nil {
print(error)
}
else {
dispatch_async(dispatch_get_main_queue(), {
// Cache to image so it doesn't need to be reloaded every time the user scrolls and table cells are re-used.
if let downloadedImage = UIImage(data: data!) {
imageCache.setObject(downloadedImage, forKey: url)
self.image = downloadedImage
}
})
}
}).resume()
}
}
...so that works great for loading collection view images. However, I also want to capture an array of the images so I can pass it onto another view controller and not need to download everything again (waste of data). I created another function to do this, but imageholder.image is always nil. I don't think it's an async issue because all the images are cached at this point and when I debug I can never get it to not be nil.
var imageHolder: UIImageView!
var imagesArray:[UIImage] = [UIImage]()
// Function to create images array to use on the photo view.
func createImagesArray() {
for url in imagesURLArray {
imageHolder.loadImageUsingCacheWithURLString(url)
if imageHolder.image != nil {
imagesArray.append(imageHolder.image!)
}
}
}
It feels like I am missing something simple, but I'm stuck. Thank you!
Upvotes: 0
Views: 5169
Reputation: 3804
I ended up create another function for the extension that returned a UIImage. After doing that there was not problem appending to the imagesArray.
let imageCache = NSCache()
var returnImage:UIImage = UIImage()
func returnImageUsingCacheWithURLString(url: NSURL) -> (UIImage) {
// First check if there is an image in the cache
if let cachedImage = imageCache.objectForKey(url) as? UIImage {
return cachedImage
}
else {
// Otherwise download image using the url location in Google Firebase
NSURLSession.sharedSession().dataTaskWithURL(url, completionHandler: { (data, response, error) in
if error != nil {
print(error)
}
else {
dispatch_async(dispatch_get_main_queue(), {
// Cache to image so it doesn't need to be reloaded everytime the user scrolls and table cells are re-used.
if let downloadedImage = UIImage(data: data!) {
imageCache.setObject(downloadedImage, forKey: url)
returnImage = downloadedImage
}
})
}
}).resume()
return returnImage
}
}
Upvotes: 2
Reputation: 1873
let imageCache = NSCache()
let images:[UIImages] = []
extension UIImageView {
func loadImageUsingCacheWithURLString(url: NSURL) {
self.image = nil
// First check if there is an image in the cache
if let cachedImage = imageCache.objectForKey(url) as? UIImage {
self.image = cachedImage
return
}
else {
// Otherwise download image using the url location in Google Firebase
NSURLSession.sharedSession().dataTaskWithURL(url, completionHandler: { (data, response, error) in
if error != nil {
print(error)
}
else {
dispatch_async(dispatch_get_main_queue(), {
// Cache to image so it doesn't need to be reloaded every time the user scrolls and table cells are re-used.
if let downloadedImage = UIImage(data: data!) {
imageCache.setObject(downloadedImage, forKey: url)
self.image = downloadedImage
}
})
}
}).resume()
}
//Creat array and append the last image to it.
self.images.append(self.image)
}
Upvotes: 0