[英]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.