Cannot set image property of UIImageView - ios

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.

Related

UIImage view does not display the image

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")

How would i go about making 4 UIImages on UIView Blink or remove based on Timing?

I dont have or know the code yet to be able to create this project but basically it would work like a Gauge meter where i have a UIView with a Background and a Test tube image where inside the Test tube image, would have 4 rectangular images stacked on top of eachother
Each having their own properties. The very bottom, the on on top and the middle and the last one on the very top.
My question is, how would i be able to remove the top one based on a NSTimer and so forth till the last Bar image is left ?
I was thinking of using an NSMutableArray to hold the images this way:
NSMutableArray *testTube = [[NSMutableArray alloc] initWithObjects:
[UIImage imageNamed:#"bar1"], // 0
[UIImage imageNamed:#"bar2"], // 1
[UIImage imageNamed:#"bar3"], // 2
[UIImage imageNamed:#"bar4"],nil];
But aftter this in the IBAction method, i did a:
[testTube removeObjectAtIndex: 3];
and it didnt remove it on the app when i clicked the IBAction Button to test it out if i can successfully manipulate the bar4.png
Where am i going wrong ? and as for the timer, it is suppose to remove them one by one in 5 minutes till there is no bar left.
What displays an image on the screen is a UIImageView (not a UIImage, as your question seems to imply). If you have one of those that you want to remove from the screen, you can do one of the following:
[imageView removeFromSuperview];
or
imageView.hidden = YES;
Here's how I would suggest to handle it:
Set up 4 image views in Interface Builder. Drag the images into your project and set up the image views to line up in your test tube. There is an image property on image views. Enter the name of each image and the image will show up. Make the image view the same size as the image it contains so it doesn't get scaled or stretched.
Then create an IBOutlets for each of your images views by control-dragging from the image view into your view controller's header. Name the top image view imageView1, the next one imageView2, the next imageView3, and the bottom one imageView4.
Define an NSArray property imageViews in your view controller's header. In your viewDidLoad method, add this line:
self.imageViews = #[imageView1, imageView2, imageView3, imageView4];
Also add a counter instance variable. Let's call it imageIndex;
When your view controller first displays, all 4 image views will be visible.
Finally, you need to start a repeating NSTimer that:
sets the hidden property on the image view at the current index to YES.
The timer method would look something like this:
- (void) hideAnImageView: (NSTimer *) theTimer;
{
self.imageViews[imageIndex].hidden = YES;
imageIndex++;
if (imageIndex + 1 > [self.imageViews count]
[theTimer invalidate];
}

Passing a Custom Cells image views image to a new view controller

I have a view controller which has a UITableView with some custom cells.
Within the custom cells I have a main UIImageView. I add a tap gesture recogniser to the image view, so that when the image is tapped the handleImageTap method is called.
Question
What I am trying to do is pass the image from the selected UIImageView (from the specific cell) into a new View Controller.
When the handleImageTap is called I run the following:
UIImageView *imageView = (UIImageView *)gestureRecognizer.view;
// send the image instead of self when firing the segue
[self performSegueWithIdentifier:#"remindMeTurnInfo" sender:imageView];
I then have the following in the prepareForSegue method:
if ([segue.identifier isEqualToString:#"remindMeTurnInfo"]) {
UIImageView *imgView = (UIImageView *)sender;
MESPlayedTurnReminderViewController *VC = segue.destinationViewController;
VC.mainImageView.image = imgView.image;
}
The above does not work the image is not passed across. How can I get the image from the selected cells image view to the new controller?
Also
Sometimes when the image view of the cell is selected the didSelectRowAtIndexPath method is called. This is not correct and should not be called when the image view is tapped. This doesn't appear to be all the time, is there anyway to ensure this is not called?
I also need a reference to the cell that the imageview is within for the prepareForSegue method. How do I get the reference for the cell that the imageview is within, when the cell is (should not be) actually selected here.
Your question has 2 problems.
First, you should not use table view cells to store data. Table view cells are view objects. They display data. When the user triggers an action on a cell, you should figure out the indexPath of the cell, use that info to look up the data in your model (the image for the selected row or section/row, in your case) and use that.
Second, you should never try to manipulate another view controller's views. Treat the other VC's views as private. This is a biggie. Reaching into another VC and touching its views violates the encapsulation of the two VCs and means that the VC doing the touching becomes dependent on the appearance of the "touched" VC. Bad. Then you can't change the appearance of the touched VC without breaking other code. It also doesn't work right after creating the new VC, because it's views don't exist yet.
Instead, you should add an "imageToUse" property to the other view controller, and set THAT in the prepareForSegue method. Then the second view controller can take the image and install it in it's image view in it's viewWillAppear method.
I changed the approach.
Putting a UIImage on the destination Controller instead. Then in the viewDidLoad of this controller I pass the image into the UIImageView. I imagine this is because the image view is not created yet.

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.

UIView::addSubView obstructs the navigation bar originally at the top

I designed a very simple interface for an ipad device: UIView + a navigation bar.
Then after the view is load, it will download an image from a location and use the following method to display it:
-(void)connectionDidFinishLoading:(NSURLConnection *)connection{
UIImage* testImg = [UIImage imageWithData:_networkData];
UIImageView* testView = [[UIImageView alloc] initWithImage:testImg];
[_view addSubview:testView];
[testView release];
}
The problem is now the new UIImage occupies the whole visible area of the Ipad.
I wonder how can I fix this issue? I suppose I have to find out the frame of the display area in the original view, and then assign it to the UIImageView instance?
initWithImage will automatically adjust the frame to match the size of the image you're passing in. To tell the UIImageView to take the same size as its parent view you could just add the following line before you add the subview:
testView.frame = _view.bounds
...we use bounds rather than frame because the superview may have an offset that we don't want the image view to have.

Resources