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).
Related
I have an static UITableView with grouped cells, and an outlet for an UIImageView in the first group and I try to set its image on viewDidLoad like this
- (void)viewDidLoad {
[super viewDidLoad];
self.productImageView.image = [UIImage imageNamed:#"someImage"];
}
I found the productImageView (UIImageView) is nil but only on iOS7 (7.1.2), as far as I knew, it shouldn't be nil after viewDidLoad
Can anyone tell me why this is the only outlet is not loading from my storyboard?
Moving your code to the viewWillAppear will likely fix your issue, but that's not really the optimal solution. What if you add a call to [self.tableView reloadData]; before you set the image to make sure that the tableView is initialized?
Well, I found the reason
I made the ViewController in a different storyboard, Xcode 6, with the new format for storyboards and different devices, but actually, our project is as old as Xcode 5 and we still use those storyboards, when I made copy & paste of the ViewController from the new storyboard to the old one, it should work because we are used to this, and it does with iOS8, but I noticed the view is shown like this in the view inspector
http://i.stack.imgur.com/ticT5.png (can't post images yet)
Seems like the image and its constraints are disabled and deleting the element, adding a new one seems to solve the problem for iOS7 and still work with iOS8
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'm working on my first more complex app and couldn't find a solution to this one. I have loaded about 64 icons into my app. For a table view, a user can assign to each cell one of these icons. Basically when he edits the cell he gets to a new UIView with all the 64 icons. The currently chosen icon should have a border and if he clicks a different one, the border should move and the icon assigned to that selected item in the tableview.
My problems are now:
a) How do I load these 64 icons into my view? I've created the view with all the UIImageViews, but how do I load them into these imageviews? Where are they saved if I copy them just to my directory and how do I access them?
b) Is there an easier way than placing 64 different UIViews into this view manually and linking them up with IBOutles?
Your problem could easily be resolved using UICollectionView . A UICollectionView just acts like a UITableView .
Go through the link in order to find more about UICollection
http://skeuo.com/uicollectionview-custom-layout-tutorial
Using UICollection view you just need the pass the image object saved in your Array
That's a lot of questions on a wide variety of topics.
How do I load icons/images?
UIImage *image = [UIImage imageNamed:#"fubar.png"];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
If you just copy images in your project they are part of the main bundle and for the most part you and pretty much ignore where they are saved. It's pretty easy to access them. (see above)
Is there an "easier way"... well, you'd have to give us a way if you want to know if there is an easier way. However I think a simple naming convention and a loop would be workable.
NSMutableArray *icons = [NSMutableArray array];
NSString *iconName;
UIImage *image;
UIImageView *imageView; = [[UIImageView alloc] initWithImage:image];
for (int count=0 ; count < 65 ; count++) {
iconName = [NSString stringWithFormat:#"icon%d.png", count];
image = [UIImage imageNamed:iconName];
[icons addObject:image];
}
Then you would use your icons array as the basis of your UITableView. About that though. You might want to look into using a UICollectionView instead. It's like a table, but it's a grid of items instead of just rows. It would lend itself better to a large set of images.
As for how you copy the images into the views, both your UITableView and your UICollectionView have methods where they "ask" for the data and give you a position. Based on the position information you just set the it asks for and it will handle the rest. It might look something like
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
// ...
// remembering 'icons' is our array of image data
cell.imageView.image = icons[indexPath.row];
// ...
}
Edit from Comments: How to tell if a file exists in the app bundle? (my answer is just a cut/paste)
[[NSFileManager defaultManager] fileExistsAtPath:pathAndFileName];
You would have to do this in code. I would suggest you put all your filenames in NSArray, then iterate through it and with each step create a UIImageView on your ViewController programmatically. I suggest though that you subclass UIButton so it is "selected" when you tap it and you can then put any image into it.
you can use gridView a open source component here is the link
GridView
Also to detect you can assign tag to each imageView like from 0 to 63 then when user choose 0th image you can assign that tag to a local variable so that next time when user views grid you can identify that user has previously chose 0th index image. I hope it helps you.
Looking for easy way to support retina displays. It occurred to me that if I could look through a nib-loaded view and get the names of all the image resources used there, I could check if they have a corresponding retina image and load it (if it's a retina device.)
I know how to iterate through subviews after it's loaded, but I don't know how (or if you can) get the resource name set in Interface Builder. I'm trying to avoid having to set all the image names in code.
What I'd like to do (in pseudo code):
for subView in self.view.subviews:
if subView is UIImageView:
resourceName = (UIImageView *)subView.imageName
if retinaResourceFileExists(resourceName) and isRetinaDisplay:
(UIImageView *)subView.image = retinaImage(resourceName)
(Bonus: Maybe there is a way to iterate through IBOutlet variables, but I doubt it?)
use [UIImage imageNamed:#"Foo"]; and the device will load the #2x image automatically.
Interface Builder loads the retina image automatically, too.
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