Here is what I'm doing, when I create an image with the path in the bundle:
UIImage *image = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:#"image" ofType:#"jpg"]];
What I want to do is trying to find the path for my image but without using the extension, without using 'ofType' (because the name of my image and her extension is store in my database) something like that:
UIImage *image = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:#"image.jpg"]];
But I don't know how to do it.
Best regards,
Why don't you split the string that you get from the DB?
NSString* fullFileName = #"image.jpg";
NSString* fileName = [[fullFileName lastPathComponent] stringByDeletingPathExtension];
NSString* extension = [fullFileName pathExtension];
Now you can simply use:
[[NSBundle mainBundle] pathForResource:fileName ofType:extension]];
You can easily use the NSBundle method without passing the extension, just pass nil for extension.
[[NSBundle mainBundle] pathForResource:#"image.jpg" ofType:nil];
- (NSData *)applicationDataFromFile:(NSString *)fileName {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *appFile = [documentsDirectory stringByAppendingPathComponent:fileName];
NSData *myData = [[[NSData alloc] initWithContentsOfFile:appFile] autorelease];
return myData;
}
taken from http://developer.apple.com/iphone/library/documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/FilesandNetworking/FilesandNetworking.html#//apple_ref/doc/uid/TP40007072-CH21-SW21
Could be easily adapted if you wanted it to return a UIImage instead of an NSData.
Also, you don't say if you are saving the images to the documents directory, or adding them to your app bundle before compiling. because if it's the latter, you can use [UIImage imageNamed:(NSString *)filename] to get the image. It expects an extension as part of the file-name.
The easiest way is to store the name and file type in your database separately, and retrieve them using the first method.I'm not sure that you can be successful in implementing the latter one.
I found that with an extension of ".jpg" it was necessary to use ofType for the extension for the app to work on an iPod Touch, whereas with an extension of ".png" I could just put "image.png" in pathForResource and say ofType:nil. But all versions worked on the simulator.
The app bundle contains the image file, and I am using:
[[NSBundle mainBundle] pathForResource:#"Auto" ofType:#"jpg"]
to get a path.
Related
I created a bundle with name applausible.bundle, i placed the bundle in supporting files.This is my code
NSBundle* myBundle;
myBundle = [NSBundle bundleWithPath:#"/Library/applausible.bundle"];
NSString *path = [myBundle pathForResource:#"splash.png" ofType:nil];
[imageName setImage:[UIImage imageNamed:path]];
Here I am unable to display the image on the image view. Another way I tried like this I place the image on the image view using xib,while running the app. The output is coming like this in console
Could not load the "splash_screen high resolution.png" image referenced from a nib in the bundle with identifier "com.xxxxxxx.ImageSample". Can any one tell me where i made a mistake.Finally my goal is to display the image on the UIImageview which is in external bundle.
This may help you.
NSString bundlePath = [[NSBundle mainBundle] pathForResource:#"applausible" ofType:#"Bundle"];
NSLog(#"Bundle path is %#",bundlePath);
NSBundle myBundle;
myBundle = [NSBundle bundleWithPath:bundlePath];
NSString *path = [myBundle pathForResource:#"splash_screen high resolution" ofType:#"png"];
UIImage *image=[[UIImage alloc]initWithContentsOfFile:path];
[imageName setImage:image];
please try it out.
It may be helpful
NSString *bundlePath = [[NSBundle mainBundle] pathForResource:#"applausible" ofType:#"Bundle"];
using this method you can get the bundle path.Then use your code from second line
myBundle = [NSBundle bundleWithPath:bundlePath];
NSString *path = [myBundle pathForResource:#"splash" ofType:n#"png"];
[imageName setImage:[UIImage imageNamed:path]];
I need to Read a Image from the specific URL .
It works fine with WWW . but it returns a nil when the URL pointing the Local Folder .
// Works
NSString *sampleData = #"http://blogs-images.forbes.com/ericsavitz/files/2011/05/apple-logo2.jpg";
// Returns nil
NSString *sampleData = #"USER/user2/...";
Note :
I am changing the NSString to NSURL and creating the UIImage .
NSURL *url = [NSURL URLWithString: data];
UIImage *image = [UIImage imageWithData: [NSData dataWithContentsOfURL:url]];
You are supplying a relative pathname for the file URL. That relative pathname is interpreted relative to the current working directory of the running application, which isn't guaranteed to be anything in particular, and so is almost certainly not what you want.
You can either supply an absolute path - one that starts with '/' - or set your app's current working directory to something explicit, like your user's Documents folder.
you probably should have a look into the NSBundle Class.
Methods like
- (NSURL *)URLForResource:(NSString *)name withExtension:(NSString *)extension subdirectory:(NSString *)subpath
or
- (NSString *)pathForResource:(NSString *)name ofType:(NSString *)extension
is probably what you want
First of all, you can NOT read file from such path you given: "USER/user2/...", the file must in your App bundle or in your App's sandbox.
Second, check your path string if there was some texts need to be encoded in URL. Try:
NSURL *url = [NSURL URLWithString:[data stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
Also, if the url is not nil, you should also check if your [NSData dataWithContentsOfURL:url]; is returning nil. If so, it means your URL is not correct so the method cannot find your file.
P.S., You are mistyping your image create code, you should call alloc before imageWithData:.
You should do something like to get the local url :
NSString *docDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *pngFilePath = [NSString stringWithFormat:#"%#/%#", docDir, nameOfFile];
and finaly, load your image :
UIImage *image = [UIImage imageWithContentsOfFile:pngFilePath];
Try these instead
NSString *path = #"USER/user2/.../xxx.xxx";
NSFileManager *fileManager = [NSFileManager defaultManager];
BOOL isFileExist = [fileManager fileExistsAtPath:path];
UIImage *image;
if (isFileExist) {
image = [[UIImage alloc] initWithContentsOfFile:path];
}
else {
// do something.<br>
}
I got annoying question...
What if I have many image , and I want load it in to a table view
And show the file name as cell's text , and the preview image is also show in the cell.
When I select the cell , it will push to next view , show the big size image.
That's it .
I don't know how to load many folder to an array?
/*********** EDIT ***********/
This is the folder I set many images inside
You can see that's only one root folder ...
And this is my code to load the image inside
NSFileManager *fileManager = [NSFileManager defaultManager];
NSString *filePath = [[NSBundle mainBundle] pathForResource:#"Image"ofType:#""];
NSDirectoryEnumerator *direnum;
direnum = [fileManager enumeratorAtPath: filePath];
imageFolder = [NSMutableArray new];
for(NSString *filename in direnum){
if([[filename pathExtension] isEqualToString:#"png"]){
[imageFolder addObject:filename];
}
}
NSLog(#"Files in the folder %#",imageFolder);
I got result like this :
Files in the folder (
"macro1.png",
"macro10.png",
"macro11.png",
"macro12.png",
"macro13.png",
"macro14.png",
"macro15.png",
"macro16.png",
"macro17.png",
"macro18.png",
"macro19.png",
"macro2.png",
"macro20.png",
"macro21.png",
"macro22.png",
"macro23.png",
"macro24.png",
"macro25.png",
"macro26.png",
"macro27.png",
"macro4.png",
"macro5.png",
"macro6.png",
"macro7.png",
"macro8.png",
"macro9.png"
)
But what if I change the root folder like this
How to read the image files in the subfolders?
The App won't have issues finding files inside the bundle. The structure of your app development folders is irrelevant to the end product, for the most part. If you are storing the images in the apps bundle, the system can find it.
UIImage *image = [[UIImage alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:#"imageName" ofType:#"png"]];
When I've had to do this as a quicky solution, I created an NSDictionary entry for each photo and stored that inside of an array in userPrefs. I programmatically created thumbnails for each image to utilize in the cell.imageView.image property, and then used an NSDictionary with #"description", #"imageName", and #"imageNameThumbnail" as the keys. You could do the same thing with an NSArray and just call the objectAtIndex, but I prefer the plain text friendliness of dictionaries.
You can try testing the path information that is being returned. Here are a couple of lines to try:
NSString *path1 = [[NSBundle mainBundle] pathForResource:#"imageName" ofType:#"png"];
NSString *path2 = [[NSBundle mainBundle] pathForResource:#"imageName" ofType:#"png" inDirectory:#"folderName"];
NSLog(#"Path 1: %#",path1);
NSLog(#"Path 2: %#",path2);
See what the output for these lines is, or if they return nothing.
Documents Directory as filePath
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectoryPath = [paths objectAtIndex:0];
Then, enumerate the documentsDirectoryPath, and it should read subfolders recursively.
If it is local data you are using, you probably want to put the data in a .plist file. You would just be putting the names of the files in there, and then load the plist into an NSDictionary which you will only have one key for called "images" or something like that. You can then load all the objects under the "images" key into an array and use that array to populate your table.
Hope this makes sense.
I want create and return a dictionary using the keys and values found in a file specified by a given path. I have my file on my Desktop:ciudades.txt (a human readable file!!! no a xml, just for practice). What method of my NSString i need to use and how? Please can somebody help me filling on my code the XXXXXXX. Thanks in advance
- (NSMutableDictionary)ciudades
{
if (!ciudades) {
NSString *path = [NSString XXXXXXXXX];
ciudades = [NSMutableDictionary dictionaryWithContentsOfFile:path];
}
Define a function.
-(NSMutableDictionary*) ReadFileAsDictionaryForPath:(NSString* ) path
{
return [NSMutableDictionary dictionaryWithContentsOfFile:path];
}
Use it as below
NSString *path = [[NSBundle mainBundle] pathForResource:#"MyFile" ofType:#"txt"];
NSMutableDictionary* myDictionary = [self ReadFileAsDictionaryForPath:path];
if(!myDictionary)
{
NSLog(#"Error while reading data from file at path:%#",path);
}
First add that file to your application bundle By adding a existing file from xcode to your project. Then use this method to get the file path for example I'm getting a image's path.
NSString *path = [[NSBundle mainBundle] pathForResource:imageName ofType:#"png"];
then try the dictionaryWithContentsOfFile method and see if it works or not.
I am trying to set image to UIImageView programmatically but it does't work. I have following code.
[image setImage:[UIImage imageNamed:[self localImagePath:NO]]];
and
-(NSString *)localImagePath:(BOOL)forSave {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSPicturesDirectory, NSUserDomainMask, YES);
NSString *picturesDirectory = [paths objectAtIndex:0];
NSString *picturesPath = [picturesDirectory stringByAppendingPathComponent:#"image.png"];
if (forSave || [[NSFileManager defaultManager] fileExistsAtPath:picturesPath]) {
return picturesPath;
}
else {
return [[NSBundle mainBundle] pathForResource:#"image" ofType:#"png"];
}
}
Is it possible to set image to UIImageView on run time from different mediums?
The imageNamed: method takes only the filename as an argument, not a path. Also it will look for that file in the main bundle only. Try using imageWithContentsOfFile: instead.
You'll have to use two different ways to load the image
[UIImage imageNamed: name] for the resource based one
[UIImage imageWithContentsOfFile: filename] for the one you found locally.
consider changing your method to return the UIImage and use the appropriate loading method internally.
Use [UIImage imageWithContentsOfFile:[self localImagePath:NO]], instead of [UIImage imageNamed:...] for the one you found using your localImagePath method.