dmytro-anokhin / url-image

AsyncImage before iOS 15. Lightweight, pure SwiftUI Image view, that displays an image downloaded from URL, with auxiliary views and local cache.
MIT License
1.1k stars 96 forks source link

URLImage Never Loads Image in Widget App for SwiftUI #88

Closed wiredag closed 3 years ago

wiredag commented 3 years ago

Summary and/or background Simple code for an iOS Widget, I have verified the meme is loaded with the meme.url value from an earlier HTTP request. I've also checked printing the url text on it's own and that works too, but I can't load the image with the URLImage package. I've tried on the widget and the contentview as well and no results. Any ideas?

meme.url is a URL type which is an optional.

URLImage: https://github.com/dmytro-anokhin/url-image

NOTE: To be more specific, the placeholder does appear, not the image from the url though.

OS and what device you are using

Version of URLImage library The version of the library where the bug happens.

What you expected would happen Image is shown in widget

What actually happens Placeholder is shown

Sample code

import SwiftUI
import URLImage

struct MemeView: View {
    let meme: MemeModel

    var body: some View {
        URLImage(meme.url!, placeholder: Image(systemName: "square"))
    }
}

Test data If you use public resource provide URLs of the images. Here is an example url: https://i.redd.it/zey35zvw4gq51.jpg

Additional information:

wiredag commented 3 years ago

Wanted to add that the problem is the call is happening Asynchronously and Widgets can't handle that. I found a workaround without URLImage in case you are interested:

`struct MemeView: View { var meme: MemeModel

var body: some View {
    Group {
        if let url = meme.url!, let imageData = try? Data(contentsOf: url),
       let uiImage = UIImage(data: imageData) {

       Image(uiImage: uiImage)
         .resizable()
         .aspectRatio(contentMode: .fill)
      }
      else {
       Image("placeholder-image")
      }
    }

}`

dmytro-anokhin commented 3 years ago

Hey,

you right, the image won't load because WidgetKit is synchronous. Here is related topic on Apple forum: https://developer.apple.com/forums/thread/652581

Currently I'm working on the next version that will have a way to download images separately from a view. This will allow using URLImage with WidgetKit. You can see progress here: #86

ChopsKingsland commented 1 year ago

I know I'm late to the party, however, does this work now?

I'm having trouble manually loading images outside of the view, so am looking for a library to help