2016-07-26 22 views
0

Ich bin relativ neu in iOS und würde mich freuen, wenn Sie herausfinden, wie Sie ein Array von Bildern erstellen, wenn Sie eine Erweiterung zum Verwalten von Downloads und Caching verwenden. Ich bin damit ein paar Stunden im Kreis herumgelaufen und dachte, es wäre an der Zeit, das Brain Trust anzuzapfen.Bild-Array mit Bildern im Cache erstellen (Swift)

Ich habe eine Verlängerung der UIImageView um so zu downloaden und Cache-Bilder:

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, die für das Laden Sammlung Ansicht Bilder funktioniert gut. Ich möchte aber auch ein Array der Bilder aufnehmen, damit ich es auf einen anderen View-Controller übertragen kann und nicht alles neu herunterladen muss (Datenverschwendung). Ich habe eine andere Funktion dafür erstellt, aber imageholder.image ist immer null. Ich denke nicht, dass es ein asynchrones Problem ist, weil alle Bilder zu diesem Zeitpunkt zwischengespeichert werden und wenn ich debugge, kann ich es nie zu Null bringen.

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!) 
     } 

    } 
} 

Es fühlt sich an, als würde ich etwas Einfaches vermissen, aber ich stecke fest. Vielen Dank!

+0

Sie verwenden nur eine Bibliothek dafür. Betrachte https://github.com/Haneke/HanekeSwift https://github.com/onevcat/Kingfisher etc. – Fattie

Antwort

0
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) 
} 
+0

Leider folge ich nicht. Ich sehe, dass du Bilder hinzugefügt hast: [UIImages] = [] (typo: UIImage). – Ben

+0

Ja, Tippfehler, mein Schlechter. Fügen Sie dann unten das Bild dem Array hinzu. – impression7vx

+0

Alles, was passiert, ist, dass Sie nach dem Aufruf von 'self.image = ...' das Bild in einem Array speichern. Badaboom Badabang – impression7vx

0

Ich habe am Ende erstellt eine andere Funktion für die Erweiterung, die eine UIImage zurückgegeben. Nachdem das erledigt war, gab es kein Problem beim Anhängen an das 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 
    } 
}