How to store images and refere to it for my iOS interface? - ios

I'm using XCode 6.
Let's say I have a Food model containing a (UIImage *)image property. I would like to display all my foods displaying images for my interface.
I have already the pictures for the foods (they are not in the Photo Library of the device but on my computer).
1) Where to put these image files (beet.png, banana.png etc... for example) in Xcode ? (is there a dedicated folder ?)
2) How to refere to this image file ? Example in my Food class, self.image = ?
Thanks a lot !

You can just drag and drop image files to your project while xcode is open.
make sure you use copy items if needed option, it is less hassle.
once you clikc finish for your files, you will see your files on bundle.
then all you need to do is call
[UIImage imageNamed:#"imageInBundle.png"];
imageNamed: will search your main bundle
or long way
NSBundle* bundle = [NSBundle bundleWithURL:[[NSBundle mainBundle]URLForResource:#"YourBundle" withExtension:#"bundle"]];
NSString *imagePath = [bundle pathForResource:#"imageInBundle" ofType:#"png"];
UIImage* image = [UIImage imageWithContentsOfFile:imagePath];

1) you can create new group inside your project in XCode and drag images to it. Check in build phases, in Copy Bundle Resources to make sure they'll be copyed in final app. 2) you refer image using imageWithName. You can use UIImageView for easier management and set this image your imageWithName. Otherwise you can implement directly images in your xib. Anyway how do you want to use images? In a table, in a collection, in a detail view? Depends on you how you want to display them and how you manage to make things work.

Related

All images in xcassets, then how load the image referenced from a nib in my bundle

In my iOS project, I creat a xcasset named:PaoPaoImages.xcassets
then I put all images into PaoPaoImages.xcassets.
And I have a xib file named:a.xib, I compile it into a.nib.
Then I put a.nib into a bundle named:paopao.bundle.
So, now, my images in PaoPaoImages.xcassets , and my nib in PaoPao.bundle. And the nib reference the images.
Then, I run my project, the nib cannot show the images.
I'm stumped... any advice?
In order to access the image from the Asset Catalog, you only need to access the name of the asset group without any extensions.
So, if you add an image named #"my-button#2x.png" to the Asset Catalog, it will create an asset group called my-button.
Now, all you have to do is access the image like so:
In coding use following line
[UIImage imageNamed:#"my-button"];
instead of
[UIImage imageNamed:#"my-button.png"];
Also, you can edit the asset group by renaming it (without renaming the images) or changing it's individual components. This will allow you to follow easier naming conventions as well as show completely different assets between different UIScreen scales without any scale checks.
In order to incorporate images for different device sizes, you may need to toggle it under the "Devices" subheading in the Asset Catalog Group's options.
==> check here code try it:-
NSBundle *myImageBundle=[NSBundle bundleWithPath: myBundlePath];
NSArray *ImageURLs=[myImageBundleURLsForResourcesWithExtension:#"png" subdirectory:nil];
NSLog(#"imageURLS: %#",ImageURLs);
=>here refrence more try it:-
Access Asset Catalog programmatically
Load image from bundle with IOS

How To Load The Image Set(The Bundle) From Images.xcasset in iOS

When I am developing apps in iOS before, I always put the image sources of the app at the root directory of the project, and then I will load them using [UIImage imageNamed:#"image.png"]. I did't even include various sizes and resolutions such as image#2x or image#3x. But I am afraid this is not a good practice, because I cannot deal with different screen resolutions.
Now, I want to make use of the Images.xcasset to store all my image sources so that I will be able to load just the bundle name, and hoping that the system will pick the image with the right resolution automatically. Therefore, I made a test and place my image set under Images.xcasset and named it as images, and then in my UIViewController.m I am trying to load the image by calling [UIImage imageName:images]. As a result, it didn't work. So I searched for an answer and found out that I should call [UIImage imageName:#"images60x60#2x.png"] in order to load the the 60pt #2x image.
But I think this still did not solve my problem, because I am still choosing which image to load. Is there a way to load the entire image set or load the image according to the resolution of the screen?
Please help. Many Thanks.
EDIT: Added Screen Shots
I use AppIcon as a test
As you said at last that you are using AppIcon as a test. Please don't AppIcon it is made for internal use. try to create you own imageSet and use that. It should work.
Two things :
As said Ankit, do not use App Icon, use your own set
do not use a file suffix. Just reference [UIImage imageName:#"MyImage"] (not [UIImage imageName:#"MyImage.png"])

Getting launch image from XCAssets using imageWithContentsOfFile:

I'm attempting to load images from my LaunchImage image set in a .xcassets file, but I don't want do use imageNamed:, as it wastes memory and the image only needs to be displayed for a couple of seconds during initial launch.
I have tried multiple approaches to this, but so far I have only been able to load them using imageNamed:.
This works:
[UIImage imageNamed:#"LaunchImage-700-568h.png"]
This doesn't work (returns null):
[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:#"LaunchImage-700-568h" ofType:#"png"]]
Is there any way to do this without adding the resources explicitly to the target (aside from the assets file)? Thanks!
Looks like the answer to your question is no.
According to XCAssets documentation.
Each set in an asset catalog has a name. You can use that name to programmatically load any individual image contained in the set. To load an image, call the platform specific class method, passing in the name of the set that contains the image. The OS will load the image from the set that is most appropriate for the current scale factor. The platform method for iOS is imageNamed:. For OS X the platform method is imageNamed:
So we must use [UIImage imageNamed:] method to load images from XCAssets catalog on iOS.
See similar question and answers here.

Impossible to load an image in xcassets on bundle

I need to include images in a static library.
I created a bundle and inserted in my images, the problem is that it seems to work if I include the images directly in the bundle, but stops working if I put in a xcassets file.
I followed many guides and searched for a solution on this site.
The most popular solution is to insert this line of code:
[UIImage imageNamed:#"MyBundle.bundle/imageName"]
but it seems not work for me
any ideas?
There are two ways to solve this,
If your app is still supporting iOs 7, you can use this category:
https://gist.github.com/serluca/e4f6a47ffbc19fccc63e
Otherwise, starting from iOs 8 Apple added a way to do this using:
+ imageNamed:inBundle:compatibleWithTraitCollection: defined here
Running the same problem. Looks like inline bundle support is broken for XCAssets in the 1-parameter imageNamed method. There's a workaround though using imageNamed:inBundle:compatibleWithTraitCollection: Be careful, this is iOS8 only !!
NSBundle *bundle = [NSBundle bundleWithPath:[[NSBundle mainBundle] pathForResource:#"static_lib_bundle_name" ofType:#"bundle"]];
UIImage *image = [UIImage imageNamed:#"image_in_the_xcassets_you_want" inBundle:bundle compatibleWithTraitCollection:nil];
NOTE : traitCollection is set to nil to pass the main screen traits as per apple docs (i don't quite get what it means though, if anyone knows please comment!).
For Swift 2.1:
let bundle = pathToBundle // define for your app or framework
if let image = UIImage(named: "drop_arrow", inBundle: bundle, compatibleWithTraitCollection: nil) {
// process image
}
Our images are placed in Images.xcassets and we had a problem with loading images in an IBDesignable. The following code did the job for the preview in Interface builder and the app as well:
NSBundle* bundle = [NSBundle bundleForClass:[self class]];
UIImage* image = [UIImage imageNamed:#"image.jpg" inBundle:bundle compatibleWithTraitCollection:nil];

Can't load images from bundle into storyboard

I've been working on a library that will be used across multiple applications. This library also includes resources like a storyboard and many images. I recently made this library into a .framework using the iOS Framework tutorial found on Github.
When I was first working on this library, it was in the form of an app. When I got most of the coding done, I made it into a framework. However, this is where my problem began.
I am able to present the storyboard (Engine.bundle/Platform.storyboardc), and the first screen shows which has a background image which loads. However, once I get to all of the other screens in this storyboard, the images on those screens don't load. What's weird is that the first screen image loads through the storyboard without having to set it in the code. All the other images from the storyboard don't load, yet they are all contained in the same bundle as the first image.
One of the next screen contains credit card images, which do not load. And on the screen after that, more credit card images don't load. However, once I set the images through the .m file, they load from the bundle and show on the screen. Yet when I use this same code on every other screen that doesn't show their respective images, the code doesn't work. Here's the code I'm using to load their respective images:
NSBundle *bundle = [NSBundle bundleWithURL:[[NSBundle mainBundle] URLForResource:#"Engine" withExtension:#"bundle"]];
NSString *filePath = [bundle pathForResource:#"front" ofType: #"png"];
backgroundImageView.image = [UIImage imageWithContentsOfFile:filePath];
Whenever the screens that don't have images loaded are presented, the following is logged to the console:
Could not load the "visa.png" image referenced from a nib in the bundle with identifier "com.example.app"
Could not load the "mastercard.png" image referenced from a nib in the bundle with identifier "com.example.app"
... and so on and so forth for each image.
And it even shows on the second credit card image screen... the screen where the images actually load. Yet, it doesn't show on the very first screen presented where the image loads.
My bundle is structured like so:
Engine.bundle/
Platform.storyboardc
front.png
visa.png
amex.png
... so on and so forth.
My question is why don't the images load? And why do some load? front.png loads, and on only one screen do the credit card images show. Yet when I use the same code to load the images on to other screens, the code doesn't work.
Sorry if this post is long... it's a lot of detail. I've just been really trying to figure this out. If I didn't explain something right, please let me know. Thanks.
Okay, it sounds like you need to set the bundle identifier for your Engine bundle.
Use something like "com.IAmSoAwesome.engine"
Then, instead of using:
NSBundle *bundle = [NSBundle bundleWithURL:[[NSBundle mainBundle] URLForResource:#"Engine" withExtension:#"bundle"]];
You can use:
NSBundle *bundle = [NSBundle bundleWithIdentifier: #"com.IAmSoAwesome.engine"];

Resources