I have declared an UIImageView as an IBOutlet and I am setting the image of the imageView as follows in viewDidLoad method
imageView.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:
[NSURL URLWithString: [productProperties objectForKey:#"image"]]]];
But this image doesn't show up at all.
productProperties is a NSDictionary which stores the url for the key "image"
I even tried storing the image locally and then setting it up like below
imageView.image = [UIImage imageNamed:#"icons_browse.png"];
But that also doesn't work.
Try removing the line of code and setting the "default" image in IB.
I know this is not a fix - but I am curious to see of there is some other reason why it is not visible - like it's set as hidden, has 0% alpha, is obscured by something else, etc.
The code - especially the second thing you tried (assuming the file is actually in your bundle and named properly) is very boilerplate and should work..
The other thing to do is to check the value being returned by [UIImage imageNamed] - and make sure it's not nil - meaning there is actually some problem loading the file. (Wrong file name, file not in bundle, etc.)
Sorry guys, it is my mistake, I am providing the wrong value for the NSURL.
got it fixed
Related
I want to display an image using a URL in an image view. I tried using this:
[cell.avatarImageView.image setImageWithURL:[NSURL URLWithString:#"http://www.innovaworks.com/wp-content/themes/innovaworks/images/home_person.png"]];
but it gives me this warning :
UIImage may not respond to "setImageWithUrl"
and the image not shown.
There is no method on UIImage to set the image with a url.
See this answer for a full description of how to set an image from the data contents of a url:https://stackoverflow.com/a/2782491/209867
The gist is:
UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:#"http://www.innovaworks.com/wp-content/themes/innovaworks/images/home_person.png"]]];
Understand that this is synchronous The scroll performance within a UITableView will be horrendous, and you should not use this! I only provide this answer for technical accuracy and comprehensiveness.
As it looks like you copied this code from somewhere, there are many open source categories that will asynchronously load images from a url.
One such open source project is SDWebImage. https://github.com/rs/SDWebImage It's a widely used project that easily allows you to asynchronously load a UIImage from a url.
-setImageWithURL: is not an UIImage method. You want to fetch the image data first, using NSURLRequest, NSURLSession, etc. (documentation). Then allocate an UIImage and use the initWithData: constructor. Then use the image view's setImage: method to set it.
Try
[cell.avatarImageView setImageWithURL:[NSURL URLWithString:#"http://www.innovaworks.com/wp-content/themes/innovaworks/images/home_person.png"]];
Or
cell.avatarImageView.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:#"http://www.innovaworks.com/wp-content/themes/innovaworks/images/home_person.png"]]];
In my app, i am having the image name in a variable, and I pass the value to the imageView as below
productsView.imageView.image=[UIImage imageNamed:[NSString stringWithFormat:#"%#",imageString]];
The image is not getting displayed,
I have printed my imagestring,
NSLog("%#",imageString);// It gets printed in log as myImage.png
But if I give the value directly, it works with out any mistake
productsView.imageView.image=[UIImage imageNamed:[NSString stringWithFormat:#"myimage.png"]];
Can anyone help me please
I think small mistake. From Your comment, image name should be
imageString = #"myimage.png", not #"myImage.png".
I have a UIImageView in a view of a StoryBoard, IBOutlet connections are made properly (have double and triple checked this), the images to display are added to the proper target, etc. (Have been debugging this for a day now) the UIImageView display images if I set them in Interface Builder.
But if I set the UIImageView.image property to a valid UIImage (I can see even the preview when debugging) loaded with any image it always shows the first image, not the new one, or if I left it blank in IB it keeps nil value.
This only happens in this view if I try the same thing in another view of the StoryBoard and the UIImageView properly shows the content of the UIImage.
Any clues will we greatly appreciated.
Edited:
Thanks for the replies, has tested also setting it from a button, the code is this code:
The property declaration is:
#property (strong, nonatomic) IBOutlet UIImageView *theRecomendationImage;
Have Tested with weak and strong, just to be sure.
The IBAction implementation (tested it gets called)
- (IBAction)changeImage {
UIImage *theImageToTint = [UIImage imageNamed:#"Sunny"];
// theImageToTint = [theImageToTint imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
self.theRecomendationImage.tintColor = [UIColor yellowColor];
self.theRecomendationImage.image = theImageToTint;
}
When debugging if you check the value of theImageToTint it is a valid image (not nil) and you can even quicklook the image. But when I assign it to the UIImageView the value is always nil. No error, just keep nil.
Thanks for your interest, and sorry for the incomplete question.
Edited:
If you execute
po self.theRecomendationImage.image
in the debug console before and after executing the assignment: self.theRecomendationImage.image = theImageToTint; the value is nil.
Edited:
If you add the UIImageView by code, it works and get updated when you assign a new image to it, this takes me to point to some Storyboard issue.
Is there any tool to verify a storyboard?
Just make sure the image format is .png instead of .jpg or any other.
I want to put an image preview on my table cells which derives from the RSS item that they load when pushed to.
I'm not sure what UIImage method to use for cell.imageView.image....
Right now it's got...
imageWithContentsOfFile
, which doesn't crash, but also doesn't display anything because....
I also want to know how best to derive the image data from the block of html, being that I'm using an open source parser (should I try to nab it in there, or create my own class for deriving it from the description string that the parser created?
I much prefer to try to create my own solutions rather than fill my project with a hundred open source library files.
Thank you for your thoughts...
UIImage* myImage = [UIImage imageWithData:
[NSData dataWithContentsOfURL: [NSURL URLWithString: #"http://example.com/image.jpg"]];
cell.imageView.image = myImage;
I want to set an image as my background at runtime, so I created a UIImageView 'backGroundImage' as IBOutlet in Interface Builder. However the following code:
UIImage *image = [UIImage imageNamed:imageName];
NSLog(#"image=nil %d",(image ==nil));
NSLog(imageName);
[self.backGroundImage setImage:image];
NSLog(#"backgroundImage=nil %d",(self.backGroundImage.image ==nil));
gives me the following NSLogs
2012-06-15 12:09:43.967 iElster[1960:207] image=nil 0
2012-06-15 12:09:43.967 iElster[1960:207] bedroom-sketch-corel-eps-vector-art-1141.jpg
2012-06-15 12:09:43.968 iElster[1960:207] backgroundImage=nil 1
so basically I can load my image, but I can not set it as the image of the UIImageView. If I include the following line
backGroundImage = [[UIImageView alloc]init];
I don't get 'nil' anymore for my backGroundImage.image - but in that case I obviously don't use the UIImageView created by the Outlet anymore. I do not see the problem with my code above, especially since all I saw as suggestions in similar Stack Overflow threads was exactly the same: "simply use the setImage method"; Any ideas what could be the problem?
PS: just to clarify that - I tried using different images, both .png and .jpg and they all work if I e.g. just set them as a subview to my ScrollView, but not when trying to use them in the code above. So the image files shouldn't pose any problems.
It seems that you placed this code in a method that is executed before the view gets loaded and the outlets are connected (e.g., in init...). Try placing it in viewDidLoad. You cannot access your view outlets before the view gets loaded (i.e., before viewDidLoad is accessed).
Also, this line:
[image release];
is a memory management error (overrelease).