UIImage view does not display the image - ios

I'm having a few issues with displaying a UIImage in an app that I'm developing. The steps I took to do this are as follows:
1 - Add a UIImageView to my main view controller in the storyboard. I did this using the interface builder.
2 - I then created an IBOutlet for the UIImageView in the viewcontroller.h. I ensured proper referencing of the image view from the outlet.
3 - I set the image for the UIImageView like so:
_fireIcon = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"flame_icon.png"]];
This was done within the viewDidLoad method in the viewcontroller.m file.
Troubleshooting:
I checked whether the _fireIcon variable is being set. It is.
I checked whether the viewDidLoad method is being called. It is.
In addition, the image is displayed when I statically set it via the storyboard itself. But when I do it via code, it does not display.
Let me know if you need any more additional information, or code snippets.

you need not to initialize an imageview as you are using Interface builder for image view
_fireIcon.image = [UIImage imageNamed:#"flame_icon.png"]; // or [_fireIcon setImage:[UIImage imageNamed:#"flame_icon.png"]];
This sets the image.
When we drop the UI elements using Interface builder , we need not initialize them, as they are taken care by apple.

Doing this you reset your property with newly created UIImageView:
_fireIcon = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"flame_icon.png"]];
And your reference to UIImageView from storyboard gets compeletely lost.
Instead you want to set image:
_fireIcon.image = [UIImage imageNamed:#"flame_icon.png"];

Firstly, you do need to allocate the Element created via Interface builder, you just need to set the image to UIImageView directly like this :-
[myImage setImage:[UIImage imageNamed:#"5.jpg"]];
Make sure image name is correct and its extension too. I just tried and its working great here. Let me know if you still face problem here.

Swift 3.0
statusImageView?.image = UIImage(named: "select.png")

Related

Cannot set image property of UIImageView

So, i have a .xib file which basically just contains a view, and within that, a scrollview with a UIImageView and a UIITextView. I have outlets connected to these subviews.
For some reason, i cannot set the image property of the image view. Below is how i instantiate the view controller in my UITableview:
MyNewViewController *ssv = [[MyNewViewController alloc]initWithNibName:#"MyNewViewController" bundle:nil];
[[self navigationController]pushViewController:ssv animated:YES];
ScreenshotInfo *currentPic = [pics objectAtIndex:indexPath.row];
UIImage * pic = [UIImage imageWithData:currentPic.pic.imageData];
ssv.imageView.image = pic;
// even doing like below does not work
//ssv.imageView.image = [UIImage imageNamed:#"fbpic.png"];
Should that not be enough?
Adding the UIImageView programatically works, but this should not be the solution.
Setting the image in viewdidload works, but i want the image data from the first view controller.
Any suggestions?
When your program run at ssv.imageView.image = pic, the property named imageView of ssv must be nil. Because the root view of ssv has not been loaded. Your code of setting image runs on one run loop, while loading view of your view contoller runs on the next run loop. This is why you setting the image in viewDidLoaded works fine. And you should setting the properties of views of nib in the method of viewDidLoaded.

Change Background Image of an iOS app with a button click

Title pretty much says it all, I am new to Xcode and am trying to make a button that changed the view's background image. I will have three button and when you click one they will change the background that is associated with the button. How would I accomplish this? I have looked around but have not found anything relevant or anything that worked..
Thanks in advance!
EDIT::
As for what I have, I have a project set up with a window and one view with a background image called "default#2x-568h#2x.png" I would like to be able to press a button and have it switch to "second.png". I have a button and I control dragged into my .h which made this:
#interface TeslameterViewController : UIViewController <CLLocationManagerDelegate> {
IBOutlet UIButton *button1;
}
- (IBAction)button1:(id)sender;
#end
thats where I am lost.
EDIT AGAIN:
Found the fix via this video:
http://www.youtube.com/watch?feature=player_embedded&v=iKuGiJMgMiQ
On the button Action event add this line.
(void)Action:(UIButton *)sender
{
UIGraphicsBeginImageContext(self.view.frame.size);
[[UIImage imageNamed:#"bg.png"] drawInRect:self.view.bounds];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
self.view.backgroundColor = [UIColor colorWithPatternImage:image];
}
try this:
self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"imageName.png"]];
and also see UIView Image Background for example project with complete code...
You need to be more specific on what you need.
Within your xib file, create a UIImageView and stretch its dimensions to fit the size of the screen. Also create a the necessary buttons. Create properties in your view controller to have a reference to the UIImageView and the buttons. Give the buttons an action that changes the picture that the UIImageView loads.
You have to create three buttons in nib file and add outlet to your view controller.
To create outlet you have right click on button then you will get list of event, then control drag to your view controller header file, then one small window will pop up, write method name and click enter. Now u will see method in your controller.write code to change background of button or your view in that method.
for more details:http://developer.apple.com/library/ios/#documentation/iphone/conceptual/iphone101/Articles/05_ConfiguringView.html

Xcode - Image view not hiding

I have an image view in a basic app that I am attempting to set to hidden on load using:
- (void)viewDidLoad
{
my_image.hidden = YES;
}
This code along with some other attribute changes are not functioning at all. I have synthesized the my_image property. Any ideas why this may not be working? Also, please let me know if you need any further information. I'm new to this and it's really bugging me, so thanks in advance!
It's hard to make it clear with a simple code line, my_image.hidden = YES;
But I think you can do the things below :
Print the imageView (your 'my_image' object) in the console to look into it.
Use the other properties of the imageView to see if you can manipulate it, such as changing the frame, setting the background color, or setting an image for it. If you can change the frame, then you may set my_image.hidden = NO; somewhere else.
Create another UIImageView object and add try it!
If none of above works, you can set the frame of 'my_image' to CGRectZero to hide it.
Maybe you forgot to connect the IBOutlet ? Are you using .xib ?
make a breakpoint at line of my_image.hidden = YES; Does it go into the breakpoint ? If it goes into , make sure the my_image is not nil.
UIImageView *imageview = [[UIImageView alloc]init];
[imageview setHidden:YES];
Setter worked for me:
UIImageView * bb = (UIImageView*)[self.view viewWithTag:1];
[bb setHidden:YES];
I had the same problem with hiding the image view but if you remove the #property statement the hidden behaves as it it should.
Set the hidden in viewdidLoad then the IBaction - works fine
.h
IBOutlet UIImageView *crackedimage1;
.m
-(void)viewdidLoad
crackedimage1.hidden = YES;
- (IBAction)crackaction1:(id)sender {
crackedimage1.hidden = NO;};

How to fully prepare a view without showing it on the screen in IOS?

I want to use iCarousel ( a library providing a coverflow scrolling style )
I have created a View with XIB. On that View I have UIImageView
And I have a Controller for that View with all the IB things set up ( 'view' of the FileOwner set to my view and there is an outlet of UIImageview )
Programmatically I create the Controller
SBTViewController* iv1 = [[SBTViewController alloc] initWithNibName:#"SBTViewController_iPhone" bundle:nil];
SBTViewController* iv2 = [[SBTViewController alloc] initWithNibName:#"SBTViewController_iPhone" bundle:nil];
and then
UIImage* im1 = [UIImage imageNamed:#"1.png"];
iv1.imView.image = im1;
UIImage* im2 = [UIImage imageNamed:#"2.png"];
iv2.imView.image = im2;
Please note that I am not adding the view of iv1 controller to be shown or be part of any hierarchy just now.
Instead, I am storing the pointer, and some time later ( when the carousel moves ) I simply give the iv1.view to the carousel to show it up.
I can see the view but not the image ...
After searches I found a question on stackoverflow where it is explained that setting image to the imageview before it was added to the views hierarchy will not actually work, meaning that I can only set up images programatticaly after ViewDidLoad.
But in my case I want to init each instance with different values, so adding code in in ViewDidLoad is not robust enough ...
Edit : I just found out that simply accessing the view paramater of the controller like this
iv1.view.tag
will cause the view to be loaded
So How should I do it ?
Hide it beneath others ? Ugly
thanks
Your problem is that when you create your UIImage, your UIImageView is not yet created. So two solutions: you create your UIImage after the viewDidLoad. But if you don"t want that, you may remove your UIImageViews from your XIB and create them yourself in the init method of your class, and then add them to your view hierarchy in the viewDidLoad.

Duplicated uiview added programmatically

I'm currently making a photo decoration app. The *holderView is the sticker that has been chosen by user. Whenever I try to load a photo from photo library or take a photo and then load back to this page, additional *holderView added programmatically, which is the sticker that I've previously chosen before taking a photo, duplicated sticker appears after that, which is not what I want it to be.
How should I write the code for not letting this happen?
Thanks a lot.
- (void)viewWillAppear:(BOOL)animated {
UIView *holderView = [[UIView alloc] initWithFrame:CGRectMake(0, 50, _imagePicker.selectedImage.size.width, _imagePicker.selectedImage.size.height)];
UIImageView *imageView = [[UIImageView alloc] initWithFrame:[holderView frame]];
[imageView setImage:_imagePicker.selectedImage];
[holderView addSubview:imageView];
...
}
Your problem seems to be that your using the viewWillAppear method instead of the viewDidLoad. This will cause multiple "imageViews" because your adding a new one every time you hide then show the viewController it's presented in. what you want to do is move the creation of the imageView (if there really is suppose to only be 1) to the viewDidLoad method and make that imageView accessible to the entire class, then in the viewWillApear simply change the image inside the imageView to the newly selected one.

Resources