Can't display UIImage in UIImageView - iphone

i want to display an UIImage within a UIImageView but it doesn't work and i don't know why.
could you please help me.
i am using the following coding:
-(void) showCorrectedImage {
MyAppDelegate *delegate = (MyAppDelegate *)[[UIApplication sharedApplication] delegate];
CorrectedImageController *imageController = [[CorrectedImageController alloc] initWithNibName:#"CorrectedImageView" bundle:nil];
UIImageView *imgView = [[UIImageView alloc] initWithImage:originalImage];
imageController.imageView = imgView;
[imgView release];
[delegate.navController pushViewController:imageController animated:YES];
}
There is a xib file called "CorrectedImageView.xib" and within the xib i have placed an UIImageView. I have connected the UIImageView outlet with the image view and the view outlet with the view.
original Image is defined as UIImage and initialized with a file.
br.,
martin

In these lines
UIImageView *imgView = [[UIImageView alloc] initWithImage:originalImage];
imageController.imageView = imgView;
[imgView release];
you're creating a new UIImageView instance, when you already have one in the XIB file. If your connections in IB are good, this should be all you need to do:
imageController.imageView.image = originalImage;
Also, please don't forget to release imageController after you push it like this:
[imageController release];
P.S. As to why your code doesn't work, the newly created UIImageView instance, while it replaces the previous one in the view controller, it's not added as a subview of the view controller's view. So after your code executes, you have:
An UIImageView instance retained as a subview of the main view.
Another UIImageView instance initialized with your image and retained in the view controller, but not a subview of the main view, and therefore not displayed.

sorry, but i don't get it running. maybe my nib file is wrong.
it looks as follows:
-File's Owner (CorrectedImageController)
-First Responder (UIResponder)
-View (UIView)
--ImageView (UIImageView9
I've connected the File's Owner view property to the View Element
and the File's Owner imageView property to the ImageView Element.
Did i miss something ?
BR,
Martin

Related

How can I set a UIImageView's image before I present the view?

I have an app that I am working on, and in an effort to save on views that I make, I want to be able to dynamically pass the view an image. So for example, I make a view:
FlipsideViewController *controller = [[FlipsideViewController alloc] initWithNibName:#"FlipsideView" bundle:nil];
Then I want to set the image that the view's imageview shows before I present it:
UIImage *image = [UIImage imageNamed: #"IMG_5010_2.jpg"];
[controller.imageView setImage:image];
[controller.label setText:#"HI"];//I am trying to do this too and it isn't working...
But it just isn't working!! Does anyone have any thoughts on this? Please help!!
Thanks
NOTE: I do have UIImageView and UILabel attributes set on the view I am trying to present...
You should set the image within viewDidLoad method of the relevant UIViewController as the view won't exist during the init phase and will have been displayed by the time viewDidAppear is called.
Perhaps you could try this:
add two new properties to your FlipsideViewController:
#property (retain) UIImage *image;
#property (copy) NSString *labelText;
Don't forget to synthesize them in your FlipsideViewController.m.
Then when you instantiate your FlipsideViewController:
FlipsideViewController *controller = [[FlipsideViewController alloc] initWithNibName:#"FlipsideView" bundle:nil];
UIImage *image = [UIImage imageNamed: #"IMG_5010_2.jpg"];
controller.image = image;
controller.labelText = #"Hi";
and then in your FlipsideViewController viewDidLoad method you can assign the values in the properties to the view IBOutlets:
- (void)viewDidLoad {
//do other stuff
[self.imageView setImage:self.image];
[self.label setText:self.labelText];
//any other stuff
}
Sure you've got your IBOutlets hooked up properly in the nib file?
Assuming you've put this code in the right place in your view controller, this should be working fine. So it's either you're not in a place that this code is getting run, or the things you're configuring aren't hooked to any objects in the nib. It has to be one of those two things.

Bug with UIView frame property?

I am initializing a new UIViewCOntroller object.
then attempting to set its view's position of stage but I am having some trouble.
here is the code I am using
Note: this code is placed in the application main UIViewController's viewDidLoad method
UIViewController * cont = [[UIViewController alloc] init];
cont.view.backgroundColor = [UIColor redColor];
CGRect rect = CGRectMake(100, 0, 320, 480);
cont.view.frame = rect;
this code is still positioning the subview at (0,0) instead of (100,0)
However, if I introduce a decimal, such as using 320.01 (for the width value) or 480.01 (for the height value). The view would be positioned correctly.
It seems that if I use a size with an exact width:320.0 height: 480.0,
the origin will always be set to (0,0) !!!
This is a bit strange. I was hoping that someone could explain why this is happening, and possibly how it may be resolved.
Cheers ....
NSLog the value of cont.view and I think you will find it to be nil, which explains why nothing's happening. This is not the normal way to create a UIViewController -- it's not wrong to create one programmatically, but 99.99% of the time UIViewController subclasses are created with the main UIView in a .xib file. A freshly created UIViewController object has a nil "view" member, so you've got to initialize it somehow, either by loading a .xib:
MyViewController *vc = [[[MyViewController alloc] initWithNibName#"MyViewController" bundle:nil] autorelease];
or manually creating the view:
MyViewController *vc = [[[MyViewController alloc] init] autorelease];
UIView *theView = [[[UIView alloc] initWithFrame:viewframe] autorelease];
vc.view = theView;
Then you can move the view's frame to your heart's content, but moving the base view of a view controller is usually not what you want to do, you want to create sub-views and move those around.
[[UIViewController alloc]init] is wrong. The designated initializer for UIViewController is initWithNibName:(NSString *)nibName bundle:(NSBundle *)nibBundle. Even that does not necessarily initialize the view outlet to an actual UIView immediately. You need to subclass UIViewController and perform your customisations in the viewDidLoad method of that subclass.
In the interim is likely that view is nil so you can try setting whatever properties of it you like without anything ever happening.
I think you should be able to use
-(void) loadView {
[super loadView];
//create your views programmatically here
}
in order to create your viewController programmatically and avoid the IB. Normally the IB calls this method for you when your 'view' property is nil, however if you're avoid the IB make sure to include the above method so your view property is not nil.

UIImageView not displaying image when property is set too early

I have an image I want to display inside a UIView. In Interface Builder, the UIView is the root and a UIImageView is its child.
The view is connected to view controller's view outlet, and the image view is connected to the image view outlet:
#property (nonatomic, retain) IBOutlet UIImageView *imageView;
If I try to set the image property of UIImageView before it's visible, the image doesn't show up.
TestView *testView = [[TestView alloc] initWithNibName:#"TestView" bundle:nil];
testview.imageView.image = [logos objectAtIndex:indexPath.row];
[self.navigationController pushViewController:testView animated:YES];
If, however, I pass the image to the controller and set the image property in viewDidLoad, the image becomes visible.
TestView *testView = [[TestView alloc] initWithNibName:#"TestView" bundle:nil];
testview.image = [logos objectAtIndex:indexPath.row];
[self.navigationController pushViewController:testView animated:YES];
- (void)viewDidLoad
{
[super viewDidLoad];
imageView.image = image;
}
What is causing the image to not show up in the first scenario?
The iOS documentation for a UIViewController's initWithNibName:bundle: method states that:
…
The nib file you specify is not loaded right away. It is loaded the first time the view controller’s view is accessed. If you want to perform additional initialization after the nib file is loaded, override the viewDidLoad method and perform your tasks there. …
A consequence of the nib/xib file not being loaded immediately (i.e. it uses so called lazy loading) means that the imageView IBOutlet property in your TestView controller is still nil when you try to set it in your first example.
In your second example you are setting it in the viewDidLoad which is called after the nib/xib file is loaded, and therefore it works as you expect.

Create a custom UIView as a tableHeaderView programmatically

I want to create a 'detail view' in my navigation-based app similar to the address book app.
I want to create a UIView that has an UIImageView and a UILabel that I can pass to a UITableVIew's tableHeaderView property when pushed by a navigation controller. The label text and image must take information from the managed object context when it loads.
I started trying to do this using IB, but go fed up when the label text wouldn't update, regardless of where I put the myView.UILabel.text = #"some new text". It just kept presenting the text I entered in the IB inspector window.
So without using IB, how would I go about creating a UIView that has a UILabel and a UIImageView in it?
Should I be creating a new class that is a sub-class of UIViewController or just UIView?
How do I go about creating the sub-views for the label and image?
Where should the text and image URL be set in code? (viewDidLoad or loadView or viewWillLoad)(UIView, UIViewController, detailViewController)?
If you started using IB and the UILabel text wouldn't update, sounds like the IBOutlet of your view controller isn't correctly connected to the label in IB.
Hopefully that will fix your problem with using IB. And in the viewController code, you should update that text in viewDidLoad.
But if you want to do this programmatically, I would make a subclass of UIView (not UIViewController).
And I would override initWithFrame and do all the setup there.
Something like the following:
myView.m
#implementation myView
#synthesize imageView, myLabel;
- (id) initWithFrame:(CGRect) frame
{
if (self = [super initWithFrame:frame]) {
// Setup your image view and add it to the view.
self.imageView = [[[UIImageView alloc] initWithFrame:initWithFrame:frame] autorelease];
self.imageView.image = ...
[self addSubview:self.imageView];
// Setup your label
self.myLabel = [[[UILabel alloc] initWithFrame:frame] autorelease];
self.myLabel.text = #"whatever you like";
[self addSubview:self.myLabel];
}
return self;
}
Make sure to clean up memory properly in the dealloc method of course depending on whether you like make class properties vs class variables.

UIImageView not displaying in UITableView header

My problem is that I can't seem to get the image from my bundle to display properly. This method is in the view controller that controls the tableview. headerView is loaded with the tableview in the .nib file and contains a few UILabels (not shown) that load just fine. Any ideas?
- (void)viewDidLoad {
[super viewDidLoad];
[[self view] setTableHeaderView:headerView];
NSBundle *bundle = [NSBundle mainBundle];
NSString *imagePath = [bundle pathForResource:#"awesome_lolcat" ofType:#"jpeg"];
UIImage *image = [UIImage imageWithContentsOfFile:imagePath];
imageView = [[UIImageView alloc] initWithImage:image];
}
If you've already created an outlet and connected it to a view in Interface Builder, you should use that view, rather than creating a UIImageView on the fly.
//this assumes that headerView is an already created UIView, perhaps an IBOutlet
//also, imageViewOutlet is an IB outlet hooked up to a UIImageView, and added as
//a subview of headerView.
//you can also set the image directly from within IB
imageViewOutlet.image = [UIImage imageNamed: #"awesome_lolcat.jpeg"];
[[self view] setTableHeaderView: headerView];
FIrst you need to figure out whether your image is loading properly. The quickest way to get an image is to use the UIImage convenience method +[UIImage imageNamed: rawImageName].
Also, is this a UITableViewController? (it's unclear, but implied).
Where is imageView being used? You create it near the bottom, but don't seem to do anything with it. You probably want to create the image view, assign it an image, and then add it as a subview to the headerView.
//this assumes that headerView is an already created UIView, perhaps an IBOutlet
UIImage *image = [UIImage imageNamed: #"awesome_lolcat.jpeg"];
UIImageView *imageView = [[UIImageView alloc] initWithImage: image];
[headerView addSubview: [imageView autorelease]];
[[self view] setTableHeaderView: headerView];
Adding the subview programatically worked, but it isn't correctly linked to the UIImageView in Interface Builder. The view I created is (I think) correctly linked to the UIView outlet in my UITableViewController, but when I init my imageView it creates a new view instead of putting the image in the view I already created.
Thanks for your answer.
See http://developer.apple.com/library/ios/#samplecode/TableViewUpdates/Introduction/Intro.html
It will display UIImageview on section header.