Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
765 views
in Technique[技术] by (71.8m points)

swiftui - iOS WidgetKit: remote images fails to load

I'm observing a bizarre thing: in the new widgets far too often remote images are not being displayed even though the image has been successfully loaded and placed in cache.

For image downloading I've tried:

  • SDWebImageSwiftUI
  • Kingfisher
  • SwURL

All of them indicate that image loading succeed, but the actual widget is not showing it.

struct TestWidgetEntryView : View {
    var entry: Provider.Entry

    var body: some View {
        WebImage(url: URL(string: "https://miro.medium.com/max/3840/0*TLqp5Uwavd-U_xrs.jpg"))
                        .onSuccess()
                        .resizable()
    }
}

On the second run of the debugger - with image loading from cache - I get the image displayed, but never(?) on initial run.

It feels like that in onSuccess I need to trigger UI-invalidation? But how?

(Since it happens to literally every image-lib I try - I don't think that it's something off in the libs)

Environment:

  • iOS 14 Beta 3 (both device and simulators)
  • Xcode 12 Beta 3
  • During the debug run, memory use is around 15mb
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Yes, as mentioned by Konstantin, it is not supported to load images asynchronously. There are 2 options to load network image in widgets

  1. Either fetch all the images in TimelineProvider and inject them to the views directly.

    OR

  2. Use Data(contentsOf: url) to fetch the image. This way it still fetches them synchronously but the code is cleaner. Sample code -

    struct NetworkImage: View {
    
      private let url: URL?
    
      var body: some View {
    
        Group {
         if let url = url, let imageData = try? Data(contentsOf: url), 
           let uiImage = UIImage(data: imageData) {
    
           Image(uiImage: uiImage)
             .resizable()
             .aspectRatio(contentMode: .fill)
          } 
          else {
           Image("placeholder-image")
          }
        }
      }
    
    }
    

This view can simply be use like this -

NetworkImage(url: url)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...