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];
Related
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.
I have array of image url. I want to show the images into UIImageView.
Now I convert the URL to NSData and then convert that into UIImage and then try to load that into UIImageView.
But it takes a lot of time to do this.
Is there a better way where in I can load the images in a faster and better manner?
Despite all of the answers on here telling you to do this in one line of code, it will sadly make no difference to the URL connection speed OR data / image decoding. If you want a faster way to TYPE the code then fine, but I would use category added to UIImageView....
#interface UIImageView (URL)
- (void)loadFromUrl:(NSString *)aUrl;
#end
#implementation UIImageView (URL)
- (void)loadFromUrl:(NSString *)aUrl {
NSURL *url = [NSURL urlWithString:aUrl];
NSData *data = [NSData dataWithContentsOfURL:url]
UIImage *image = [UIImage imageWithData:data];
if(image != nil) {
[self setImage:image];
}
}
#end
Now you can include the header and do...
[myImageView loadFromUrl:#"http://myurl.com/image.jpg"];
For more categories (I will be adding this one to my list!) check here. Those are all my useful ones, you may find them useful too! :)
Use everything in a single statement.
UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:MyURL]]];
[UIImage imageWithData:[NSData dataWithContentsOfURL:]]
UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:imageUrl]];
try this:-
image is UIImage and imageView is UIImageView
NSData *receivedData = [NSData dataWithContentsOfURL:#"yoururl"];
self.image=nil;
UIImage *img = [[UIImage alloc] initWithData:receivedData ];
self.image = img;
[img release];
[self.imageView setImage:self.image];
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"];
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.
I have a working UIImageView, but I want to change it based on a certain variable so I have this:
a string called imagevariable(which holds the value of image1 which is a png resource in my project).
So I have this:
imageView.image = [UIImage imageWithContentsOfFile: [[NSBundle mainBundle] pathForResource:#"imagevariable" ofType:#"png"]];
But it doesn't work. Can someone help?
By wrapping the variable name imagevariable in quotes it is being treated as a string literal, try this instead
imageView.image = [UIImage imageWithContentsOfFile: [[NSBundle mainBundle] pathForResource:imagevariable ofType:#"png"]];
You are passing image variable as a string which means, that you will literally search for an image named "imagevariable.png". Instead take away that #"" and make it like this:
imageView.image = [UIImage imageWithContentsOfFile: [[NSBundle mainBundle] pathForResource:imagevariable ofType:#"png"]];