UIImageView not displaying in UITableView header - iphone

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.

Related

Issue in assigning image to UIImageView iPhone

In my .h file I have:
IBOutlet UIImageView *image1;
and in .m file I am trying to assing image to this image1 like this
image1 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"Blue-Jaanamaz-Small"]];
But the UIImageView is not showing anything. Its blank.
What can be the problem.
If you have an IBOutlet attached to your property , you do not need to allocate and initialize the imageView again.
Just try :
image1.image = [UIImage imageNamed:#"Blue-Jaanamaz-Small"];
Also use this if your imageView is not on the top of your view hierachy.
[self.view bringSubviewToFront:image1];
Make sure that your imageView is not hidden in InterfaceBuilder or somewhere in code.
And make sure your IBOutlet is correctly connected to an imageView in the InterfaceBuilder.
Check the connections between Interface builder and UIImageView.

How to reset the image in UIImageView without loading the whole view?

I have a xib which has 1 UIImageView and 1 Toolbar.
Toolbar has two buttons save and reset.
In the simulator i can make some drawing on the image in the UIImageView.
Now i want that if i click on the reset button then i would get the original image if i didn't save the changes on the image.
I don't want to load the entire view again.
Only I need to replace my edited image with original image?
Can you please guide me that how can i do this stuff?
IBOutlet UIImageView *imageView;
imageView.image=[UIImage imageNamed:#"img.png"];
[imageView setNeedsDisplay];
UIImageView *imageView;
imageView.image=[UIImage imageNamed:#"one.png"];
refresh with orignal image. Have u tried this
[imageView reloadInputViews];
If it not work try this code again to refresh
imageView.image=[UIImage imageNamed:#"one.png"];
Create an IBOutlet using
IBOutlet UIImageView *myImageView;
Hook this up with your xib file in interface builder and change the image with
myImageView.image=originalImage;
[myImageView setNeedsDisplay];
You can use myImageView to refer to that image view in IB.
Can you just set the image property of the UIImageView to the original image.
UIImageView *imageView = [UIImageView new];
[imageView setImage:origingalImage];

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.

Can't display UIImage in UIImageView

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

Custom UINavigationBar Background

I've been trying to set up a custom background for the whole of my NavigationBar (not just the titleView) but have been struggling.
I found this thread
http://discussions.apple.com/thread.jspa?threadID=1649012&tstart=0
But am not sure how to implement the code snippet that is given. Is the code implemented as a new class? Also where do I instatiate the UINavigationController as I have an application built with the NavigationView template so it is not done in my root controller as per the example
Uddhav and leflaw are right. This code works nicely:
#interface CustomNavigationBar : UINavigationBar
#end
#implementation CustomNavigationBar
-(void) drawRect:(CGRect)rect
{
UIImage *image = [UIImage imageNamed: #"myNavBarImage"];
[image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
}
#end
// this can go anywhere
+(UINavigationController*) myCustomNavigationController
{
MyViewController *vc = [[[MyViewController alloc] init] autorelease];
UINavigationController *nav = [[[NSBundle mainBundle] loadNibNamed:#"CustomNavigationController" owner:self options:nil] objectAtIndex:0];
nav.viewControllers = [NSArray arrayWithObject:vc];
return nav;
}
You have to create CustomNavigationController.xib and put a UINavigationController in it and change the navigationBar class to "CustomNavigationBar".
You must use the 'appearance' proxy to change the background and other styling properties of controls such as UINavigationBar, UIToolBar etc. in iOS 5.xx. However, these are not available for iOS 4.xx so for backwards compatibility, you need a hybrid solution.
If you want to support both iOS 4.xx and iOS 5.xx devices (i.e. your DeploymentTarget is 4.xx), you must be careful in wrapping the call to the appearance proxy by checking at runtime if the 'appearance' selector is present or not.
You can do so by:
//Customize the look of the UINavBar for iOS5 devices
if ([[UINavigationBar class]respondsToSelector:#selector(appearance)]) {
[[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:#"NavigationBar.png"] forBarMetrics:UIBarMetricsDefault];
}
You should also leave the iOS 4.xx workaround that you may have implemented. If you have implemented the drawRect workaround for iOS 4.xx devices, as mentioned by #ludwigschubert, you should leave that in:
#implementation UINavigationBar (BackgroundImage)
//This overridden implementation will patch up the NavBar with a custom Image instead of the title
- (void)drawRect:(CGRect)rect {
UIImage *image = [UIImage imageNamed: #"NavigationBar.png"];
[image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
}
#end
This will get the NavBar look the same in both iOS 4 and iOS 5 devices.
You just have to overload drawRect like that :
#implementation UINavigationBar (CustomImage)
- (void)drawRect:(CGRect)rect {
UIImage *image = [UIImage imageNamed: #"NavigationBar.png"];
[image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
}
#end
Implementing a category is not advisable. iOS5 may provide relief for this issue. But for old APIs, you can -
Subclass UINavigationBar to say CustomNavBar and implement the custom drawRect from Lithium's answer.
For all IB based UINavigationControllers, provide CustomNavBar as custom class for their UINavigationBar.
For all code based UINavigationControllers. Create a XIB with a UINavigationController and do step two. Then provide a factory method in code that loads the UINavigationController from the nib and provide an IBOutlet.
Eg.
[[NSBundle mainBundle] loadNibNamed:#"CustomNavigationController" owner:self options:nil];
UINavigationController *navController = self.customNavigationController;
navController.viewControllers = [NSArray arrayWithObject:controller]
You can also override the drawLayer:inContext: method in a UINavigationBar category class. Inside the drawLayer:inContext: method, you can draw the background image you want to use.
- (void) drawLayer:(CALayer *)layer inContext:(CGContextRef)context
{
if ([self isMemberOfClass:[UINavigationBar class]] == NO) {
return;
}
UIImage *image = (self.frame.size.width > 320) ?
[UINavigationBar bgImageLandscape] : [UINavigationBar bgImagePortrait];
CGContextClip(context);
CGContextTranslateCTM(context, 0, image.size.height);
CGContextScaleCTM(context, 1.0, -1.0);
CGContextDrawImage(context, CGRectMake(0, 0, self.frame.size.width, self.frame.size.height), image.CGImage);
}
And as a complete demo Xcode project on customizing the appearance of UINavigationBar this and this might be helpful.
Implementing a category won't work in iOS5, you should use Uddhav Kambli's advice for using CustomNavbar on iOS ≤ 5.
I just found this blog entry, describing this topic very simple: http://web0.at/blog/?p=38
it helped me a lot, they use the "drawRect" method to get the customisation of the background.
To all those who are having trouble with UINavigationBar custom backgrounds in iOS5, do this in the corresponding viewDidLoad methods:
#if defined(__IPHONE_5_0) && __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_5_0
if ([self.navigationController.navigationBar respondsToSelector:#selector( setBackgroundImage:forBarMetrics:)]){
[self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:#"TitleBar"] forBarMetrics:UIBarMetricsDefault];
}
#endif
Notice that in my case, the background image was named "TitleBar". You can put whatever your custom background image name is.
The problem you'll have is that if you use a navigation controller, the title of each page will overlay your custom navbar. If your navbar contains a logo or the name of your app, this is obviously unacceptable.
You could set the title of each view in your navigation stack to blank, but some views force a title that you can't do anything about (like the photo picker). So you might want to create an alternate navbar image with the same color or design as your logo navbar, but with a blank area to make room for overlaid titles.
To switch navbar images at will, add a property to your app delegate to hold the name of the navbar image and replace the first line of the first example above with these two:
YourAppDelegate* theApp = (YourAppDelegate*)[[UIApplication sharedApplication] delegate];
UIImage* image = [UIImage imageNamed:theApp.navBarName];
Then in the first view controller that you'll push onto the navigation stack, do something like this:
- (void)viewWillAppear:(BOOL)animated
{
YourAppDelegate* theApp = (YourAppDelegate*)[[UIApplication sharedApplication] delegate];
theApp.navBarName = #"navBar_plain";
}
Then in the root view controller, do the same thing but specify your logo-bearing navbar image, so it gets restored when the user navigates back to it and there is no conflicting title.
Another approach is to Use UINavigationController's delegate.
It doesn't require subclassing/overwriting the UINavigationBar class:
/*
in the place where you init the navigationController:
fixer = [[AENavigationControllerDelegate alloc] init];
navigationController.delegate = fixer;
*/
#interface AENavigationControllerDelegate : NSObject <UINavigationControllerDelegate>
#end
#implementation AENavigationControllerDelegate
#define bgImageTag 143
- (void)navigationController:(UINavigationController *)navigationController
didShowViewController:(UIViewController *)viewController
animated:(BOOL)animated
{
//this is for the future for implementing with the appearance api:
if ([[navigationController navigationBar] respondsToSelector:#selector(setBackgroundImage:forBarMetrics:)])
{
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
[[navigationController navigationBar] setBackgroundImage:[UIImage imageNamed:#"header-logo-bg.png"]
forBarMetrics:UIBarMetricsDefault];
});
}
else
{
UIImageView* imageView = (UIImageView*)[navigationController.navigationBar viewWithTag:bgImageTag];
if(!imageView)
{
UIImage *image = [UIImage imageNamed:#"header-logo-bg.png"];
imageView = [[[UIImageView alloc] initWithImage:image] autorelease];
imageView.tag = bgImageTag;
}
[navigationController.navigationBar insertSubview:imageView atIndex:0];
}
}
#end
https://gist.github.com/1184147
In iOS5, zPosition value (of UINavigationBar's most depth layer) is changed. So if you change that zPosition, the old way works.
eg.
UINavigationBar *_bar = navigationController.navigationBar;
// Insert ImageView
UIImage *_img = [UIImage imageNamed:#"navibar.png"];
UIImageView *_imgv = [[[UIImageView alloc] initWithImage:_img] autorelease];
_imgv.frame = _bar.bounds;
UIView *v = [[_bar subviews] objectAtIndex:0];
v.layer.zPosition = -FLT_MAX;
_imgv.layer.zPosition = -FLT_MAX+1;
[_bar insertSubview:_imgv atIndex:1];
This script handle view's layer, so You should import QuartzCore.
Here is an alternative solution that lets you use your own custom subclass of UINavigationBar:
https://gist.github.com/1253807
As Apple itself has said, it is not correct to override methods in Categories. So the best way to customize the background of UINavigarionBar is subclassing and override -(void)drawInRect: method.
#implementation AppNavigationBar
- (void)drawRect:(CGRect)rect
{
UIImage *patternImage = [UIImage imageNamed:#"image_name.png"];
[patternImage drawInRect:rect];
}
To use this customized UINavigationBar it should be set as navigationBar property of your UINavigationBarController. As you know this property is readonly. So what should be done is:
- (void)viewDidLoad
{
[super viewDidLoad];
AppNavigationBar *nav = [AppNavigationBar new];
[self setValue:nav forKey:#"navigationBar"];
}
It works for both iOS 5 and 4.3.
You can subclass UINavigationBar and enable it like this, since categories for drawRect won't work in iOS5 anymore
navigationController = [[((^ {
NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:[NSKeyedArchiver archivedDataWithRootObject:navigationController]];
[unarchiver setClass:[SAPHUINavigationBar class] forClassName:#"UINavigationBar"];
[unarchiver setClass:[UIViewController class] forClassName:NSStringFromClass([navigationController.topViewController class])];
return unarchiver;
})()) decodeObjectForKey:#"root"] initWithRootViewController:navigationController.topViewController];
For a static view (no animation at all), I use the default iOS setBackgroundImage
But when I have a view that's animated (resize most likely), I create a custom UIImageView and add it to the navigationBar so that I have more flexibility over it
The thing is if you just add it, it will get on top of the buttons and the titleView, so I manually save a copy of most of subviews, remove them from parent view, add my imageView and than add all the subviews back
This works for me
UIImageView *navBackground = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"navigationBackgroundSample.jpg"]];
UIView *tempTitleView = [[self.navigationBar.subviews objectAtIndex:1] autorelease];
[[self.navigationBar.subviews objectAtIndex:1] removeFromSuperview];
[self.navigationBar addSubview:navBackground];
[self.navigationBar addSubview:tempTitleView];
self.navigationBar.clipsToBounds = YES;
[navBackground release];
In this case, I don't have buttons and I found out that my titleView is at index 1, if you have buttons, they should be around somewhere in the subviews array of navigationBar
I don't know what's at index 0, I don't know if this can work around the case you have text title neither...