在 iOS 14 小部件上渲染图像

2023-09-09移动开发问题
0

本文介绍了在 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 小部件上渲染图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

Cocos2D 2.1 及宽后缀
Cocos2D 2.1 and wide suffix(Cocos2D 2.1 及宽后缀)...
2024-08-11 移动开发问题
2

无法将文件从捆绑包复制到 iOS 中的文档目录
Can`t copy file from bundle to documents directory in iOS(无法将文件从捆绑包复制到 iOS 中的文档目录)...
2024-04-15 移动开发问题
4

如何复制“字典"在斯威夫特?
How to copy a quot;Dictionaryquot; in Swift?(如何复制“字典在斯威夫特?)...
2024-04-15 移动开发问题
10

Swift - 迭代结构对象时如何对其进行变异
Swift - How to mutate a struct object when iterating over it(Swift - 迭代结构对象时如何对其进行变异)...
2024-04-15 移动开发问题
7

如何使用 Swift 将文本复制到剪贴板/粘贴板
How to copy text to clipboard/pasteboard with Swift(如何使用 Swift 将文本复制到剪贴板/粘贴板)...
2024-04-15 移动开发问题
9

Swift 无法使用类型为“([Score],Score)"的参数列表调用“find",其中 Sco
Swift Cannot invoke #39;find#39; with an argument list of type #39;([Score], Score)#39; where Score is a struct(Swift 无法使用类型为“([Score],Score)的参数列表调用“find,其中 Score 是一个结构)...
2024-04-15 移动开发问题
6