生成自定义UIApplicationShortcutIcon而不捆绑图像。

时间:2016-05-29 00:39:53

标签: swift xcasset uiapplicationshortcutitem

我目前正在UIApplicationShortcutItem工作,但是,我想使用自己的自定义图标。

我知道我可以使用iconWithTemplateImageName来获取自己的自定义图标。但是,我宁愿不在应用程序中包含100个图标。似乎iconWithTemplateImageName要求图片文件包含在xcassets

我有一个功能,可以在用户动态添加UIApplicationShortcutItem时将图像下载并保存到Document文件夹。有没有办法重新拍摄新下载的图像。

我如何添加UIApplicationShortcutItem

let icon = UIApplicationShortcutIcon(templateImageName: fileName)
let newItem = UIMutableApplicationShortcutItem(type: "Chance", localizedTitle: title, localizedSubtitle: chances.joinWithSeparator(", "), icon:  icon, userInfo: ["Items":chances])
existingShortcutItems.append(newItem)
UIApplication.sharedApplication().shortcutItems = existingShortcutItems

其中fileName是我保存的图像的路径。我已经尝试了完整路径和文件名。当然,因为它不在xcassets

中而无法工作

有没有办法在运行时将图像添加到xcassets?

有没有办法动态使用图像?

代码的工作版本:

let icon = UIApplicationShortcutIcon(type: .Favorite)
let newItem = UIMutableApplicationShortcutItem(type: "Chance", localizedTitle: title, localizedSubtitle: chances.joinWithSeparator(", "), icon:  icon, userInfo: ["Items":chances])
existingShortcutItems.append(newItem)
UIApplication.sharedApplication().shortcutItems = existingShortcutItems

0 个答案:

没有答案