iOS Development

ios – Obtain and show array of UIImages SwiftUI

Spread the love


I’m a newbie in SwiftUI and am attempting to load and show a number of photos horizontally without delay. Once I debug, it appears to be like just like the platformLogos exists and the ForEach loop is operating, however nothing seems on the display screen. The .border(.blue) is just not showing both.

Any suggestion can be useful. Thanks!

import UIKit

class ImageLoader: ObservableObject {
    
    non-public static let imageCache = NSCache<AnyObject, AnyObject>()
    
    @Printed var picture: UIImage? = nil
    @Printed var photos: [UIImage]? = nil
    
    public func downloadImage(url: URL) {
        let urlString = url.absoluteString
        
        if let imageFromCache = ImageLoader.imageCache.object(forKey: urlString as AnyObject) as? UIImage {
            self.picture = imageFromCache
            return
        }
        
        URLSession.shared.dataTask(with: url) { (information, res, error) in
            guard let information = information, let picture = UIImage(information: information) else {
                return
            }
            DispatchQueue.principal.async { [weak self] in
                ImageLoader.imageCache.setObject(picture, forKey: urlString  as AnyObject)
                self?.picture = picture
            }
        }.resume()
    }
    
    public func downloadImages(urls: [URL]) {
        for url in urls {
            let urlString = url.absoluteString
            
            if let imageFromCache = ImageLoader.imageCache.object(forKey: urlString as AnyObject) as? UIImage {
                self.photos?.append(imageFromCache)
                return
            }
            
            URLSession.shared.dataTask(with: url) { (information, res, error) in
                guard let information = information, let picture = UIImage(information: information) else {
                    return
                }
                DispatchQueue.principal.async { [weak self] in
                    ImageLoader.imageCache.setObject(picture, forKey: urlString  as AnyObject)
                    self?.photos?.append(picture)
                }
            }.resume()
        }
    }
}
@ObservedObject var logoLoader: ImageLoader = ImageLoader()
@ObservedObject var gameDetail: GameDetail = GameDetail()

var physique: some View {
    VStack (alignment: .main) {
        platformLogoView(platformLogos: self.logoLoader.photos)
        .onAppear {
            if let logoURLs = self.gameDetail.sport?.platformLogosUrls {
                self.logoLoader.downloadImages(urls: logoURLs)
            }
        }
    }
}
struct platformLogoView: View {
    var platformLogos: [UIImage]?
    let rows = [GridItem(.fixed(30)), GridItem(.fixed(30))]
    
    var physique: some View {
        Grid {
            ForEach(platformLogos ?? [UIImage](), id: .self ) { emblem in
                GridRow {
                    Picture(uiImage: emblem)
                        .resizable(resizingMode: Picture.ResizingMode.stretch)
                        .aspectRatio(contentMode: .match)
                        .clipped()
                }
                Divider()
            }
        }
        .border(.blue)
    }
}

I created the downloadImages perform and easily adopted the identical logic because the singular downloadImage perform, however appending the loaded picture to an array. I used to be pondering of utilizing lazyHGrid as an alternative of Grid however neither of them labored. I’m merely not seeing something on display screen.

Leave a Reply

Your email address will not be published. Required fields are marked *