iOS : Load image from plist file - iphone

I am trying to show various images from a plist file into UIImageView , I wrote my code like this :
NSDictionary *picturesDictionary = [NSDictionary dictionaryWithContentsOfFile:[[NSBundle mainBundle] pathForResource:#"Photos" ofType:#"plist"]];
imageArray = [picturesDictionary objectForKey:#"PhotosArray"];
PhotosInAlbum.image = [UIImage imageWithContentsOfFile:[imageArray objectAtIndex:1]];
But actually nothing happens, and my ImageView is empty ! also I have two buttons to forward backward images .
Contents of Plist :

The plist is incorrect, it should be
Root ---- Dictionary
PhotosArray ----- Array
Item 0 ------ String -- Beach.jpg
Then add this code to your viewController.. Be sure that the Interface imageView is link to IBOutlet.
NSDictionary *picturesDictionary = [NSDictionary dictionaryWithContentsOfFile:[[NSBundle mainBundle] pathForResource:#"Photos" ofType:#"plist"]];
NSArray *imageArray = [picturesDictionary objectForKey:#"PhotosArray"];
PhotosInAlbum.image = [UIImage imageNamed:[imageArray objectAtIndex:0]];

PhotosInAlbum.image = [UIImage imageWithContentsOfFile:[imageArray objectAtIndex:1]];
this part should be like this
PhotosInAlbum.image = [UIImage imageNamed:[imageArray objectAtIndex:1]];

Try using this:
PhotosInAlbum.image = [UIImage imageNamed:[imageArray objectAtIndex:1]];
Your [imageArray objectAtIndex:1] will only return image name not its path, so in order to use imageWithContentsOfFile you have to specify the path of your image not only image name.

imageWithContentsOfFile: use a path that is the full or partial path to the file.
You can use imageNamed: method instead (which use the name of the file). Just replace
[UIImage imageWithContentsOfFile:[imageArray objectAtIndex:1]];
to
[UIImage imageNamed:[imageArray objectAtIndex:1]];

initially, [UIImage imageNamed:[imageArray objectAtIndex:1]]; would have solved ur problem.
But you are not able to resolve this issue this way which looks like
the problem seems to be with this line
[controller.view addSubview:PhotosInAlbum];
it might be the case your controller.view is nil at the moment you are trying to add ur imageview.
just make sure it isn't. cos if it is then your PhotosInAlbum is not at all getting added as subview to the controller.view.

Related

UIImage from file problem

I am trying to load an saved image but when I check the UIImage it comes back as nil. Here is the code:
UIImage *img = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:#"/var/mobile/Applications/B74FDA2B-5B8C-40AC-863C-4030AA85534B/Documents/70.jpg" ofType:nil]];
I then check img to see if it is nil and it is. Listing the directory shows the file, what am I doing wrong?
You need to point to the Documents directory within your app then like this:
- (NSString *)applicationDocumentsDirectory
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *basePath = ([paths count] > 0) ? [paths objectAtIndex:0] : nil;
return basePath;
}
Usage:
UIImage *img = [UIImage imageWithContentsOfFile:[NSString stringWithFormat:#"%#/70.jpg",[self applicationDocumentsDirectory]]];
First, you are using pathForResource wrong, the correct way would be:
[[NSBundle mainBundle] pathForResource:#"70" ofType:#"jpg"]
The whole idea of bundling is to abstract the resource path such as that it will always be valid, no matter where in the system your app resides. But if all you want to do is load that image I would recommend you use imageNamed: since it automatically handles retina resolution (high resolution) display detection on the iPhone for you and loads the appropriate resource "automagically":
UIImage *img = [UIImage imageNamed:#"70.jpg"];
To easily support regular and retina resolution you would need to have two resources in your app bundle, 70.jpg and 70#2x.jpg with the #2x resource having both doubled with and height.
Try loading a UIImage with:
[UIImage imageNamed:#"something.png"]
It looks for an image with the specified name in the application’s main bundle. Also nice: It automatically chooses the Retina (xyz#2x.png) or non-Retina (xyz.png) version.
Your path simply wont work because your app is in a sandbox, and you are trying to use the full path.
You should be using the following instead:
UIImage *img = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:#"70" ofType:#"jpg"]];
or you can use, but is slower than the above:
UIImage *img = [UIImage imageNamed:#"70.jpg"];

IPhone UIImageView Image

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.

Help needed in the following code

Can we assign the value of an object of an array having an image value to a variable of image view, see the following code
NSArray *imgArray=[[NSArray alloc] initWithObjects:#"Bingo2.png", nil];
UIImageView *img=[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
img.image=[imgArray objectAtIndex:0]; //line 3
[self.view addSubview:img];
its not working, Application is crashing i guess because of line 3
Please help me,
Many Thanks for the help.
You are storing an NSString object in the array and not an image. That is why it is crashing.
use this
img.image= [UIImage imageNamed:[imgArray objectAtIndex:0]];
instead of
img.image=[imgArray objectAtIndex:0];
if the above doesn't work
than you can also use this
NSString *string = [NSString stringWithFormat:#"%#", [imgArray objectAtIndex:0]];
img.image= [UIImage imageNamed:string];
You have a string of a filename in your array, not an image. Use [UIImage imageNamed:#"Bingo2.png"] instead.

UIImageView animationImages strange behavior

I'm populating the animationImages field of my UIImageview via NSArray arrayWithObjects. When I put objects in the array via UIImage imageNamed: the UIImageview animates as expected. However when I put objects in the array via UIImage alloc initWithContentsOfFile I don't see the animated images (I just see the default image I pass to initWithImage for the UIImageview). Any ideas?
Cheers!
That is because imageNamed: and initWithContentsOfFile: both look in different directories. imageNamed: looks in your bundle directory, initWithContentsOfFile: does not.
If you have code like this:
NSString *file = #"image.png";
UIImage *image = [UIImage imageNamed:file];
But you want to use initWithContentsOfFile:, you should use this code:
NSString *file = [[NSBundle mainBundle] pathForResource:#"image" ofType:#"png"];
UIImage *image = [[UIImage alloc] initWithContentsOfFile:file];

How to load images from the Documents folder that have been saved dynamically?

I have problem with loading images from the Documents folder of iPhone application into the tableView.
In a separate request, I check on the server all the images available and download them into the "images" folder under Documents. I am pretty sure that the images are saved correctly.
NSString *filePath = [imagesFolderPath stringByAppendingPathComponent:imageFileName];
[urlData writeToFile:filePath atomically:NO];
NSLog(#"Saved to file: %#", filePath);
2010-01-22 17:07:27.307 Foo[2102:207] Saved to file: /Users/Hoang/Library/Application Support/iPhone Simulator/User/Applications/6133A161-F9DC-4C92-8AE6-5651022EAA94/Documents/images/86_2.png
[NSBundle mainBundle] is not suitable for loading the images because at runtime, the application tries to connect to the server to download the images, they are not static.
But loading the images from Documents/images folder does not give me the image on the TableView.
static NSString *CellIdentifier = #"CellCategory";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
// Set up the cell...
UIImageView *imageView = cell.imageView;
MenuItem *item = (MenuItem *) [arrayMenuItems objectAtIndex:indexPath.row];
cell.textLabel.text = item.descrizione;
NSString *strImagePath = [[imagesFolderPath stringByAppendingPathComponent:[NSString stringWithFormat:#"%d_%d", item.idItem, item.idNode]] stringByAppendingPathExtension:#"png"];
NSLog(#"strImagePath: %#", strImagePath);
imageView.image = [[UIImage imageWithContentsOfFile:strImagePath] autorelease];
2010-01-22 17:07:42.842 Foo[2102:207] strImagePath: /Users/Hoang/Library/Application Support/iPhone Simulator/User/Applications/6133A161-F9DC-4C92-8AE6-5651022EAA94/Documents/images/86_2.png
Is there anyone having the same problem?
I have looked around in stackoverflow but have not succeeded.
Thanks in advance.
I've just had the same problem with loading images from application's Documents folder to UITableViewCell. This works:
[UIImage imageWithContentsOfFile:fullPath];
hardcoding the path is a bad idea since it can change during a redeploy,
try something like this..
NSString *imagessDirectory = [NSHomeDirectory() stringByAppendingPathComponent:#"Documents/images"];
and verify the file/image is even there with nsfilemanager
Edited: ANSWER
Be sure to check the response on NSData to see if there are images. In my case, all codes are ok, but the response from server give nothing. It is still able to save the image to the file on the documents/images folder, and still not raise any error, until I realized that all the images are not exist on the server. THAT WAS THE ERROR, not relates anything to the Documents folder
Original answer
There must be some problems with the initialization code of UIImage.
I have tried already three initialization functions for image having the path on the Documents directory.
But it just does not work.
Here you can see, the code always fall into the (exist) block, the first line is to load the image from the Documents/images directory, it always fails.
The second line inside the (exist) block, I tried to get the image from the bundle, it works perfect, but it is not what I want.
Fourth line of code, I get the original link of the images on the server and it gets what I nearly want. (Actually, what I want is to save all the images from the server into the Documents/images directory before hand, and then load them from there)
NSString *strImagePath = [[imagesFolderPath stringByAppendingPathComponent:[NSString stringWithFormat:#"%d_%d", item.idItem, item.idNode]] stringByAppendingPathExtension:#"png"];
NSLog(#"strImagePath: %#", strImagePath);
BOOL exists = [[NSFileManager defaultManager] fileExistsAtPath:strImagePath];
if (exists){
//UIImage *menuImage = [UIImage imageWithContentsOfFile:strImagePath];
//UIImage *menuImage = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:#"ALT" ofType:#"png"]];
//imageView.image = menuImage;
NSString *strImagePathURL = [NSString stringWithFormat:#"http://foo.com/%d_%d.png", item.idItem, item.idNode];
NSData *imageData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:strImagePathURL]];
imageView.image = [[UIImage alloc] initWithData:imageData];
}
else {
imageView.image = [UIImage imageNamed:#"ALT.png"];
}