LampShade
LampShade

Reputation: 2775

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? Say I create a new application. Then add a today extension project to the solution. And then I create a 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.

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

The IMG_3799.JPG is inside the main application in the References folder of that project. 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.

Upvotes: 3

Views: 2161

Answers (3)

ji sung Choi
ji sung Choi

Reputation: 163

I'm afraid this might be late. Following is what I did with Xcode 12.4

First, go into your Assets.xcassets file in Xcode then select the image you want to be shared. Second, click "Hide or show the Inspectors" in the top right corner of Xcode. Thir, click "Show the file inspector" Then you should see Target Membership where you can simply click the checkbox to have the images to be used in main app, extensions, etc.

I found this method for Wigetkit from 17:10 of this youtube video: https://www.youtube.com/watch?v=ZzBBDrFNXmk

Upvotes: 4

NSExceptional
NSExceptional

Reputation: 1390

I came across this answer while looking for a way to share images between my main app and my iMessage extension. 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.

enter image description here

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.

Upvotes: 1

Ivan I
Ivan I

Reputation: 9990

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.

Upvotes: 1

Related Questions