IPhone UIImageView Image - iphone

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.

Related

iOS : Load image from plist file

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.

what's wrong in this initialization

I want to load uimage with image in path like this
/var/mobile/Applications/429E283A-6E89-4BCE-861F-252C5327F711/Library/Application Support/Students/43y. F./1.2.840.113564.3.1.2.180.0.0.90.20081228203734483950/1.2.840.113564.10.1.226230117146661685815524478531105858244/Thumbnail.bmp
I used the following code
UIImage* theImage = [UIImage imageNamed:ThumbnailPath];
if (theImage !=nil) {
cell.imageView.image = theImage;
}
else {
NSLog(#"Error");
}
where ThumbnailPath is the path
but the uiimage always nil, and I am sure the file exists , can any one please fix it for me
Best regards
UIImage* theImage = [UIImage imageWithContentsOfFile:ThumbnailPath];
if (theImage !=nil) {
cell.imageView.image = theImage;
}
else {
NSLog(#"Error");
}
hope this helps
I dont thing UIImage imageNamed can load from .bmp files..Read this and this SO threads about handling .bmp files in iphone
Refer UIImage class for load image.
How are you getting the value for "ThumbnailPath"
Are you using + pathForResource:ofType:inDirectory: method under NSBundle class.
If not consider using that.
If yes please edit your question and include the part of your code where you set the value for ThumbnailPath.
May Be this can help you
+(UIImage*)getImageFromDocumentDirectoryWithName:(NSString*)imageName
{
NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDir = [documentPaths objectAtIndex:0];
NSString *aPath= [documentsDir stringByAppendingPathComponent:imageName];
UIImage *myimage=nil;
NSFileManager *fileManager = [NSFileManager defaultManager];
if([fileManager fileExistsAtPath:aPath])
{
myimage =[UIImage imageWithContentsOfFile:aPath];
}
return myimage;
}
You can use add this functon in desired class and use it accordingly

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"];

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];

pathForResource? without using extension (Iphone)

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.