简体   繁体   English

在应用程序和今日小部件之间共享图像资源

[英]Share image resources between app and today widget

Can I share resources (images that are compiled into the application) between my main iOS app and my today extension widget? 我可以在我的主要iOS应用程序和今天的扩展小部件之间共享资源(编译到应用程序中的图像)吗? Say I create a new application. 说我创建一个新的应用程序。 Then add a today extension project to the solution. 然后将今天的扩展项目添加到解决方案中。 And then I create a UIImageView. 然后创建一个UIImageView。 I want to reference an image from a shared pool of images rather than having 2 of every image, one added to main app and another added to widget app. 我想引用共享图像池中的图像,而不是每个图像中都包含2个,一个添加到主应用程序,另一个添加到小部件应用程序。

imageView.Image = UIImage.FromBundle("IMG_3799.JPG");

The IMG_3799.JPG is inside the main application in the References folder of that project. IMG_3799.JPG在该项目的References文件夹中的主应用程序中。 I'd prefer the same image asset to be referenced by the widget. 我希望小部件引用同一图像资产。 Is that possible? 那可能吗? Currently, i'm making a 2nd copy of IMG_3799.JPG and putting it into the widget. 目前,我正在复制IMG_3799.JPG的第二个副本,并将其放入小部件中。

Out of box, you can't. 开箱即用,您不能。

The closest thing to that would be to create an app group that contains the main app and the extension and that has access to the common folder. 与之最接近的是创建一个包含主应用程序和扩展名并有权访问公用文件夹的应用程序组。 However you can't place any file there during deployment, just during runtime, so it may or may not be a workaround for what you require, but that is the only way. 但是,您不能在部署期间(仅在运行时)在此放置任何文件,因此它可能无法满足您的要求,但这是唯一的方法。

I came across this answer while looking for a way to share images between my main app and my iMessage extension. 我在寻找一种在主应用程序和iMessage扩展程序之间共享图像的方法时遇到了这个答案。 As it turns out, just by adding the image files to both targets I was able to use one single copy of my images in both places. 事实证明,只要将图像文件添加到两个目标中,我就可以在两个地方使用我的图像的一个副本。

在此处输入图片说明

In the project navigator, I added the images to a folder (not .xcassets) under my main project (not the extension) and just ensured that the Target Membership was set to both the main app and extension. 在项目导航器中,我将图像添加到主项目(而非扩展名)下的文件夹(非.xcassets)中,并确保将目标成员资格设置为主要应用程序和扩展名。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用Core Data + iCloud sync时在应用程序和Today小部件之间共享数据的正确方法 - Correct way to share data between app and Today widget when using Core Data + iCloud sync 如何使用NSUserDefaults与包含应用程序共享数据? - 今日Extension Widget - How to share data with containing app using NSUserDefaults ? - Today Extension Widget 我们可以在Today- Widget-Extension中共享App类吗 - Can we share App class in Today- Widget-Extension 与今天小部件共享plist文件 - share plist file with today widget 在Swift中的应用和今日扩展之间共享核心数据数据库 - Share Core Data database between app and today extension in Swift 无法在应用程序和今日扩展之间共享数据 - Can't share data between app and today extension (iOS Swift)在应用程序和Today Extension之间共享EKEventStore和访问 - (iOS Swift) Share EKEventStore and access between app and Today Extension 在Swift中的主应用程序和Today小部件/扩展之间共享UI元素 - Sharing UI elements between main App and Today widget/extension in Swift 除了“ UserDefaults”之外,还有什么其他方法可以在iOS中的“今日小工具扩展”和主“容器应用程序”之间共享数据 - Is there any other way than `UserDefaults` to share data between `Today Widget Extension` and the main `Container Application` in iOS 今天的小部件和应用程序之间进行通信 - communicate between today widget and application
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM