2

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. Here is an example url: https://i.redd.it/zey35zvw4gq51.jpg

import SwiftUI
import URLImage

struct MemeView: View {
    let meme: MemeModel

    var body: some View {
        URLImage(meme.url!, placeholder: Image(systemName: "square"))
    }
}
pawello2222
  • 46,897
  • 22
  • 145
  • 209
Dag
  • 69
  • 6

1 Answers1

2

Figured it out. No Need for URLImage. Problem is widgets cannot handle asynchronous calls so you have to wrap it like so:

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

}

Dag
  • 69
  • 6
  • 6
    No, you did not *figured it out*. You just copy-pasted [this answer](https://stackoverflow.com/a/63387751/8697793) from the duplicate question. – pawello2222 Oct 01 '20 at 22:32
  • 1
    @pawello2222 Save it for the semantics dome, E.B. White! I also copy-pasted that ;) Sorry for not being specific, what I figured out is URLImage uses widgetkit which therefore is synchronous so then I looked up problems with widgetkit and found that answer and tweaked it a bit. Didn't think you needed all that info. If you wanna debate a theseus paradox as well though i'm in. – Dag Oct 02 '20 at 14:33