Image displayed using CGRect is obscuring UIButtons - iphone

I have an image displayed using CGRect (code below). the problem is that it's obscuring UIButtons I have in the nib file.
CGRect myImageRect = CGRectMake(0.0f, 40.0f, 480.0f, 280.0f);
UIImageView *myImage = [[UIImageView alloc] initWithFrame:myImageRect];
[myImage setImage:[UIImage imageNamed:recipe.image]];
[self.view addSubview:myImage];
[myImage release];
if I make the CGRect smaller, the UIButtons are there.
I can't figure out how to fix this, any help is very appreciated.
thanks

You need to send your subview to the back:
[self.view sendSubviewToBack:myImage];

Related

Setting retina background images appear zoomed in

I'm setting a random background image for my app by loading an image and adding it to my view:
UIImageView *background = [[UIImageView alloc]
initWithImage:[UIImage imageNamed:
[NSString stringWithFormat:#"bg%u.jpg", 1+arc4random_uniform(15)]]];
[self.view addSubview:background];
[self.view sendSubviewToBack:background];
My images are 640x1136 at 326 ppi, yet the image is appearing zoomed in for some reason. Any ideas as to why would be appreciated.
Simulator
Actual image:
Thanks.
It s because you alloc init with your image, not with a fix size.
int screenHeight = [UIScreen mainScreen].bounds.size.height;
int screenWidth = [UIScreen mainScreen].bounds.size.width;
UIImageView *background = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, screenWidth, screenHeight)];
background.image = [UIImage imageNamed:[NSString stringWithFormat:#"bg%u.jpg",1+arc4random_uniform(15)]]
[self.view addSubview:background];
[self.view sendSubviewToBack:background];
Do it like this
Hi try like this,
UIImageView *background = [[UIImageView alloc]
initWithImage:[UIImage imageNamed:
[NSString stringWithFormat:#"bg%u.jpg", 1+arc4random_uniform(15)]]];
background.frame = self.view.frame; // this is the only line you have missed
[self.view addSubview:background];
[self.view sendSubviewToBack:background];

Adding an UIImageView and a Button

I have added an imageview, and i am trying to add an button over it. How to do it.
self.imgview = [UIImageView alloc]initWithImage:[UIImage imageNamed:#"f.png"]];
[self.imgview addSubView:button];
[self.view addSubView:imgView];
What i get is the f.png Image, i dont see the button that i added over it. How could i resolve this ?
I have no idea what is wrong and i have been stuck in this problem for hours. I need your help
You need to set the views' frames:
self.imgview = [UIImageView alloc]initWithImage:[UIImage imageNamed:#"f.png"]];
self.button = [[UIButton alloc] init];
[imgview setFrame:CGRectMake(100,100,100,100)];
[button setFrame:CGRectMake(100,100,100,100)];
[self.view addSubView:imgView];
[self.view addSubView:button];
self.imgview = [UIImageView alloc]initWithImage:[UIImage imageNamed:#"f.png"]];
[self.view addSubView:imgView];
[self.view addSubView:button];
Just add them both to the view, adding the button after the image so that it composes on top of the image. Assuming their frame's x,y coordinates also match up.
Try to use bringSubviewToFront method

How to add an image subview to a tableview programmatically

I'm new to programming, and I know how to add a static image behind a tableView in a .xib file, but not in code. I have tried the following code:
UIImage * image = [UIImage imageNamed:#"pic.png"];
[self.view addSubView: image];
but nothing happens when I write this!
To display an UIImage you have to use an UIImageView and put it under the UITableView using the same frame. Don't forget to set the table view's background to transparent.
UIImageView *iv = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"pic.png"]];
iv.frame = CGRectMake(0.0, 0.0, 300.0, 600.0);
UITableView *tv = [[UITableView alloc] initWithFrame:iv.frame style:UITableViewStylePlain];
tv.opaque = NO;
tv.backgroundColor = [UIColor clearColor];
[self.view addSubView: iv];
[self.view addSubView: tv];
[iv release];
[tv release];
You need to use a UIImageView, not a UIImage. Also, when adding a view programmatically, don't forget to set the frame.

how to display Image in ImageView?

I am new to programming and developing my first application, but I want to know that how can I display image to imageView.
Programmatically :
From http://www.iphoneexamples.com/:
CGRect myImageRect = CGRectMake(0.0f, 0.0f, 320.0f, 109.0f);
UIImageView *myImage = [[UIImageView alloc] initWithFrame:myImageRect];
[myImage setImage:[UIImage imageNamed:#"myImage.png"]];
myImage.opaque = YES; // explicitly opaque for performance
[self.view addSubview:myImage];
[myImage release];
Where myImage.png is an png format image which you will import to your resources folder.
Using Nib(Xib File):
Open your.xib file.
Go to Tools -> Library.
Drag and drop one UIImageView on your .xib.
Open Attribute Inspector of the ImageView(Press Cmd + 1).
You'll get one Image property. Set the image you want from your Resource folder using the drop-down list.
There you got it. :-)
// First way
myImage.image = [UIImage imageNamed:#"myImage.png"];
// Second way
NSString *fullpath = [[[NSBundle mainBundle] bundlePath] stringByAppendingString:#"/myImage.png"];
myImage.image = [UIImage imageWithContentsOfFile:fullpath];
Check out UIImageView class reference here
Read http://www.iphoneexamples.com/
CGRect myImageRect = CGRectMake(0.0f, 0.0f, 320.0f, 109.0f);
UIImageView *myImage = [[UIImageView alloc] initWithFrame:myImageRect];
[myImage setImage:[UIImage imageNamed:#"myImage.png"]];
myImage.opaque = YES; // explicitly opaque for performance
[self.view addSubview:myImage];
[myImage release];
UIImageView *imageView=[[UIImageView alloc] init];
[imageView setImage:[UIImage imageNamed:#"yourImage.png"]];
(If you have created imageView from IB and have outlet-ed it then ignore First Line)
Try it using:
imgView.image = [UIImage imageNamed:#"test.png"];

how to show image on right hand side of table view for iphone

i can show image on left hand side of table view but why my right hand side is not working
CGRect frame;
frame= CGRectMake(310 ,10, 50, 50);
UIImageView * myImageView = [[UIImageView alloc]init];
cell.myImageView.image = [UIImage imageNamed:#"bowl.png"];
myImageView.frame = frame;
[myImageView release];
Try this, it works for me:
UIImageView *image = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"yellow_dot.png"]];
cell.accessoryView = image;
Do this code in willDisplayCell and it should work. Also remember the default cell already has an imageview, you can just move its frame.