0

I try to create an Array of UIImages from a URL Array received from a JSON Request to show them afterwards in a UITableView. But somehow my UIImage Array stays Empty and is not receiving any Data. The other Arrays for example memeURL are receiving all Data correct but memePics.count stays on 0.

Would be great if someone could show me what i am doing wrong. Also if for this task there is a better way on how to do it - it would be also appreciated!

Var:

var memePics: [UIImage] = []

Loop to add Images to Array:

while(i < memeURL.count) {
            MemeAPI.requestAPIImageFile(url: memeURL[i]) { (image, error) in
                guard let image = image else {
                    print("PIC IS NIL")
                    return
                }
                self.memePics.append(image)
                i+=1
            }
        }

RequestAPIImageFile Function:

class func requestAPIImageFile(url: URL, completionHandler: @escaping (UIImage?, Error?) -> Void) {
        let task = URLSession.shared.dataTask(with: url) { (data, response, error) in
            guard let data = data else {
                completionHandler(nil, error)
                return}
            let downloadedImage = UIImage(data: data)
            completionHandler(downloadedImage, nil)
        }
        task.resume()
    }
5
  • 1
    Are you getting images from two different api's? Commented May 11, 2020 at 16:06
  • 1
    When you say images are empty, does it show ""PIC IS NIL" ? Commented May 11, 2020 at 16:07
  • 1
    Not related: while(i < memeURL.count) { doSomethingAsync {i+1} } => DispatchGroup, enter, leave, notify. Commented May 11, 2020 at 16:08
  • @JoakimDanielson: No its the same API just 2 different Functions - first (not showed in my example) for receiving Data like URL of the IMG, Name etc - this one is working fine. And the other Function for Downloading the Image from URL. koen: No - the Console does not Print out an Error or my Custom Print which means the guard seems to pass fine. Commented May 11, 2020 at 16:13
  • I still don't understand, in the first closure for the MemeAPI call you are adding the result to an UIImage array and in the second closure for URLSession you are creating an UIImage instance so to me it looks like you are getting your images from 2 different sources. Commented May 11, 2020 at 16:19

1 Answer 1

1

Add plus line out of callback

  self.memePics.append(image)
  }
  i+=1

and use DispatchGroup to be notified on finish like

let g = DispatchGroup()
memeURL.forEach {  
        g.enter()
        MemeAPI.requestAPIImageFile(url:$0) { (image, error) in
            guard let image = image else {
                print("PIC IS NIL")
                return
            }
            self.memePics.append(image) 
            g.leave()
        }
} 
g.notify(queue:.main) {
   print("All done")
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks so much it is working now! I´ve now put both of my Functions in the Dispatch Group and the arrays works fine. Still have 1 Problem but I will create another Post for this - Thanks again!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.