Add images to Assets.xcassets programmatically

Is it possible to add images and associated meta data to Assets.xcassets programmatically so that images may be updated/added while the app is running?


Thank you for any and all suggestions


Regards

Graeme

Post not yet marked as solved Up vote post of wispaws Down vote post of wispaws
5.8k views

Replies

Is it possible to add images and associated meta data to Assets.xcassets programmatically so that images may be updated/added while the app is running?

No. Resources like this are baked into your app and you can’t modify them (and if you could it would break your code signature).

What are you trying to do here? In most cases there are alternative ways to access a resource outside of the asset catalogue. For example,

+[UIImage imageNamed:]
, which gets an image resource, has an analogue,
+imageWithContentsOfFile:
, which gets the image from a file.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

Thanks Quinn, on further investigation I've managed to achieve the results I was after... i.e. dynamically setup images at run-time to use for ARKit Image Recognition. For others that find this question here is a few sample lines of code demonstrating the solution...


Instead of encoding images in xcassets at build time to use as image recognition targets, build a image reference object from embedded or downloaded images. The sample lines below read embedded images in the App bundle but obviously they can be downloaded from a server, camera, photo library or other resource dependant upon requirements.


// create two image objects

UIImage * sample_image1 = [UIImage imageNamed:@"imac-landscape.png"];

UIImage * sample_image2 = [UIImage imageNamed:@"jaguar.jpg"];


// use the image objects to create two ARReferenceImages providing orientation and real world image size in meters

ARReferenceImage * sampleImage1 = [[ARReferenceImage alloc] initWithCGImage:sample_image1.CGImage orientation:kCGImagePropertyOrientationUp physicalWidth:0.47628f];

ARReferenceImage * sampleImage2 = [[ARReferenceImage alloc] initWithCGImage:sample_image2.CGImage orientation:kCGImagePropertyOrientationUp physicalWidth:0.25f];


// assign the two ARReferenceImage objects to the ARWorldTrackingConfiguration.detectionImages

ARWorldTrackingConfiguration *configuration = [ARWorldTrackingConfiguration new];

[configuration setDetectionImages:[NSSet setWithObjects:sampleImage1, sampleImage2, nil]];


// You are ready to perform the scanning and image recognition operation as if the images were held in an xcassets object as described in the Apple example.

thanks for the explanations..i am unity developer and i want to generate markers dynamically. after modifing native code, i do able to download an image and generate markers dynamically with this code. how to know which marker is tracked because i cant assign name to refrenceimage.

iOS, in and of itself, does not let you assign a name to an image that you just downloaded (image names can only reference system images or images baked into your app). It’s possible that Unity has a mechanism to do this. If such a mechanism does exist, it’ll be Unity-specific and I recommend that you ask about that possibility via Unity’s support channel.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"