Image retrieve from folder with folder name in iOS - ios

I have all my images in folder name ImageMe and I like to retrieve image with this name like
[UIImage imageNamed:#"ImageMe/bulk-female.jpg"];
how I get this approach, because I rereiving path name from data base, and in that format it's coming.

If you do not want to modify these images, just create folder name "Resource" under supporting files and save ImageMe folder with your images there. Then use below code to get path of image folder.
NSString *theResourcePath = [NSBundle mainBundle] resourcePath];
NSString *theFilePath = [theResourcePath stringByAppendingPathComponent:#"ImageMe/bulk-female.jpg"];

If you have the images in the main bundle you can get them with
[UIImage imageNamed:#"theNameOfTheImage_WithOutDirecotry"];.
You have to specify only the image name not the directory. This is because when the application is packed in .ipa only the images are copied (the images that are in the Copy Bundle Resources) not the directories.
If you want to pack the images in to one folder you have to create a new bundle but you can not use imageNamed you have to use initWithContentsOfFile:(NSString *)path
Here are some docs UIImage, NSBundle, Bundle Programming Guide

If your all images are present in bundle then you can directly get that images from name.
like this
[UIImage imageNamed:#"1.jpg"];
[UIImage imageNamed:#"2.jpg"]
[UIImage imageNamed:#"3.jpg"]
directly get the images using their names.
And if you want to laod that images to particular folder of bundle.
NSString *Path = [NSBundle mainBundle] resourcePath];
NSString *FilePath = [Path stringByAppendingPathComponent:#"ImageMe/bulk-female.jpg"];
try this one this might be helpful to you.

Add your images in a floder & than add that folder to your project like this
after adding the folder to your project, you can access your images with the following code.
NSString *bundlePath = [NSBundle mainBundle] bundlePath];
NSString *imagePath = [bundlePath stringByAppendingPathComponent:#"ImageFolder/imagename.jpg"];
Hope this will solve your problem

Related

xcode save image to a specific file in library

I would like to save an image to the Assets library with a specific name. Or to the Photos library. I think it's the same thing. I would like to be able to save the file like "file1.png". So far I tried with writeToFile method but I couldn't get the path right.
Hope this helps you:
NSString *path = [NSHomeDirectory() stringByAppendingPathComponent:#"Documents/Test.png"]; // identity the home directory and file name
[data writeToFile:path atomically:YES];
NSString *imageFile = [#"file://" stringByAppendingString:path];
NSLog(#"----imageFile path--%#",imageFile);

NSbundle pathforresource not finding file

I have images.xcassets listed ounder copy bundle resources, and I did try to just state the file name by itself: MSB_big_icon , before trying to add the path within images.xcassets.
Can anybody tell me what I'm doing wrong?
NSString *path = [[NSBundle mainBundle]pathForResource:#"/Raymio_android_images/MSB_big_icon.imageset/MSB_big_icon" ofType:#"png"];
NSLog(#"path: %#", path);
MSBIcon *tilecon = [MSBIcon iconWithUIImage:[UIImage imageWithContentsOfFile:path] error:&error];
David Ansermot is right that xcassets is a much better approach and strongly preferred. If you can't use that (running on older versions of iOS for instance), still put everything in one directory and use imageNamed:. This has significant caching benefits over hand-loading the file.
An asset catalog (xcassets) is a (relatively) new, unified way of managing image resources. The images are no longer accessible as separate files on the disk. Instead, imageNamed: consults the asset catalog and fetches the correct asset.
Prior to asset catalogs (and still, for non-images), assets were stored in localized directories. All of your unlocalized assets would be put into a directory called Resources (no matter where those files might appear to be in your source tree, and no matter how those files might be arranged in your Xcode folders). Localized files would be stored in directories like English.lproj or French.lproj. When you make NSBundle calls to load MyImage, it looks at each localized directory in the order the user has configured, and if it cannot find it in any of those directories, it looks in Resources.
Now it is possible to store full directories as "a resource" by marking them as directory references in Xcode. In that case, the whole directory would be copied into Resources or the appropriate localized directory. In order to find files inside such a directory you can use the ...inDirectory: version of the NSBundle methods.
So most of the time, you want to just use imageNamed:, which is going to fetch things out of the asset catalog if available, and then search localized directories, and then look in Resources. If you need to find a non-image, or if for some reason you want the real path to the file, you can compute it like this:
NSString *path = [[NSBundle mainBundle] pathForResource:#"MSB_big_icon" ofType:#"png"];
And if that resource were in a directory tree (because it was a directory reference in Xcode), you can access it like this:
NSString *path = [[NSBundle mainBundle] pathForResource:#"MSB_big_icon"
ofType:#"png"
inDirectory:#"Raymio_android_images/MSB_big_icon.imageset"];
Here's a code exemple from one of my apps :
NSString *appKey = #"Applications__GENERIC";
NSString *path = [[NSBundle mainBundle] pathForResource:appKey ofType:#"plist"];
appData = [NSDictionary dictionaryWithContentsOfFile:path];
The "Applications__GENERIC.plist" is stored like this :
Other solutions :
Use the images.xcassets.
Then in your code to load an image, use the code :
UIImage *image = [UIImage imageNamed:#"MyImageWithoutExtension"];
Don't put any path or extension, only the image's name
Try using this:
NSString *path = [[NSBundle mainBundle] pathForResource:#"MSB_big_icon" ofType:#"png" inDirectory:#"Raymio_android_images/MSB_big_icon.imageset"];
What you can also do to debug is to print out
[[NSBundle mainBundle] bundleURL]
Then navigate to that folder and see if the folder structure corresponds to the path you use.
I just struggled with this today, hope it works out for you too.

Can I access all images in a .xcassets at once?

I have a big amount of images in a Images.xcassets and I need to load them all in a UITableView. But I don't wat to write each name for the [UIImage imageNamed:#"imageName"].
What i'm looking for is something like:
NSArray *images = [MagicClass loadAllImagesIn:#"images.xcassets"];
Do you know if this is even possible? If not maybe i'll create a .bundle...
Thanks!!
Images.xcassets copies all the images in bundle, and it mixes up with other images, better follow the instructions in image below
By adding the folder with the option Create folder reference will actually create a folder in main bundle with its content, and all your images will be separated. And using the following code you can access all the images.
NSString *dirPath = [[[NSBundle mainBundle] bundlePath] stringByAppendingPathComponent:#"images"];
NSError * error;
NSArray * images = [[NSFileManager defaultManager]
contentsOfDirectoryAtPath:dirPath error:&error];
Hope it helps.

iOS Static Library with Image resources

I have a static library where I need some graphics resources as Images.
In my class I would to add an image in this way:
[UIImage imageNamed:#"myImage.png"]
but it not work...
how can I do it?
You can't provide anything other than code with a static library. This is because .a files are a simple archive of object files, with the added ability under Mac/iOS of supporting multiple CPU architectures.
The only option you have is to create a Framework, which can provide code as well as other resources.
Once you have that bundle, you can access to the resources you have there like this:
NSString *bundlePath = [[NSBundle mainBundle] pathForResource:#"YourBundle" ofType:#"bundle"];
NSString *imagePath = [[NSBundle bundleWithPath:bundlePath] pathForResource:#"yourImage" ofType:#"png"];
UIImage *image= [[UIImage alloc] initWithContentsOfFile:imagePath];
In Xcode, if you link a static library, the code from the library is included directly into your executable. No files are copied to the application bundle, so there's no way to copy the image.
If you have the image file with your static library, you can simply copy it to your application bundle by adding a copy files build phase to your target in Xcode.
I found the solution ..
I explored the bundles directory (compiled) and I've noticed that my images (.png) they had all the iestensione .tiff
To avoid this I set the key HIDPI_COMBINE_IMAGES in my bundle buid setting as NO
..so I can recover my pictures as suggested by e1985
Make a Framework target, not static library.
Make sure "Copy Bundle Resources" exists in Build Phases for your Framework. Add your resource files there.
Then to find the file in the Framework bundle from an outside project I just do this:
NSArray<NSBundle *> * bundles = [NSBundle allFrameworks];
NSString * filePath;
for (int i = 0; i < bundles.count; i++) {
filePath = [[bundles objectAtIndex:i] pathForResource:#"picture" ofType:#"png"];
if (filePath != nil) break; // found
}

What is the basic file structure in an iOS application?

I'm trying to load a plist file in my application with
NSBundle* bundle = [NSBundle mainBundle];
NSString* plistPath = [bundle pathForResource:#"CategoryData" ofType:#"plist"];
categoryProps = [[NSArray alloc] initWithContentsOfFile:plistPath];
but the categoryProps array always ends up with 0 objects. I've placed the CategoryData.plist file under the "Supporting Files" dir in my project but I can't figure out how files are arranged in the compiled app.
Can someone point me to docs that describe how the file system of an app is laid out and how to figure out where files are located within the file system?
I forgot to point out that I am using XCode 4 which does not create a resources folder for the project
Your loading code code should work for locating the file within the file system. In a project, I have:
NSString *data = [[NSBundle mainBundle] pathForResource:#"data" ofType:#"plist"];
What I would do is log the plistPath to the console or inspect it in the debugger, then navigate to that location on disk and determine if the plist ends up where you think it does.
Also, locate your application bundle in /Users/<# Username #>/Library/Developer/Xcode/DerivedData/<# Unique Appname #>/Build/Products/Debug-iphonesimulator/<# Appname #>.app, right click on it and select "Show package Contents". Ensure that you see your plist where you think you should.
You need to place your plist file in the Resources folder. Then you will be able to load and use them like this
NSString *path = [[NSBundle mainBundle] bundlePath];
NSString *finalPath = [path stringByAppendingPathComponent:#"Info.plist"];
NSDictionary *plistData = [[NSDictionary dictionaryWithContentsOfFile:finalPath] retain];
UPD: In xcode4 you must to place plist files in the "Supporting Files" directory instead of "Resources". And try to use NSDictionary instead of NSArray

Resources