在 iOS 14 小部件上渲染图像

Render Images on iOS 14 Widgets(在 iOS 14 小部件上渲染图像)
本文介绍了在 iOS 14 小部件上渲染图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发 iOS 14 小部件扩展.

I'm developing a iOS 14 Widget extension.

我有 3 张图片要循环显示并创建一个时间线来显示这些图片.

I have 3 images to show in loop and creating a Timeline to show those images.

func getTimeline(in context: Context, completion: @escaping (Timeline<Entry>) -> ()) {
    var entries: [ImageEntry] = []
    let currentDate = Date()
    
    for i in 1 ..< 4 {
        let imageNumber = String(i)
        let currentImage = "image_" + imageNumber
        let entry = ImageEntry(date: currentDate,image: currentImage)
        entries.append(entry)
    }

    let nextUpdateDate = Calendar.current.date(byAdding: .minute, value: 5, to: currentDate)!
    let timeline = Timeline(entries: entries, policy: .after(nextUpdateDate))

    completion(timeline)
}


struct WidgetModuleEntryView : View {
var entry: Provider.Entry

var body: some View {
    Image(entry.image)
        .resizable()
        .scaledToFill()
    }
}

始终呈现第一张图像,但不呈现后续图像.

First image always rendering but subsequent images are not.

可能是什么问题?

推荐答案

构建时间线需要为每个条目指定明确的条目日期.政策参数用于重新创建下一个时间线.

Constucting timeline you need to specify explicit entry date for each entry. The policy parameter is for recreating next timeline.

所以你的 getTimeline 应该看起来像

So your getTimeline should look like

func getTimeline(in context: Context, completion: @escaping (Timeline<Entry>) -> ()) {
    var entries: [ImageEntry] = []
    let currentDate = Date()

    for i in 1 ..< 4 {
        let imageNumber = String(i)
        let currentImage = "image_" + imageNumber
        let entryDate = Calendar.current.date(byAdding: .minute, value: (i-1)*5, to: currentDate)!
        let entry = ImageEntry(date: entryDate, image: currentImage)
        entries.append(entry)
    }

    let timeline = Timeline(entries: entries, policy: .atEnd)
    completion(timeline)
}

这篇关于在 iOS 14 小部件上渲染图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Why local notification is not firing for UNCalendarNotificationTrigger(为什么没有为UNCalendarNotificationTrigger触发本地通知)
Pushing UIViewController above UITabBar(将UIView控制器推送到UITabBar上方)
Dropbox Files.download does not start when number of files in folder is gt; 1000(当文件夹中的文件数为1000时,Dropbox Files.Download不会启动)
appearance().setBackgroundImage Not Working On Custom Class(外观().setBackoundImage在自定义类上不起作用)
Show/Hide barButtonItem(显示/隐藏barButtonItem)
Didn#39;t find class quot;androidx.core.widget.DrawerLayoutquot;(未找到类androidx.core.widget.DrawerLayout(q;))