UIImageView Background Image and Rotation Help - iphone

In my iPad app I currently have a background image png of 1024x1024 pixels and set it using the following
UIImageView *imgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"background-image"]];
[self.view addSubview:imgView];
[self.view sendSubviewToBack:imgView];
[imgView release];
This adds the UIImageView fine and the view is centered in landscape however it is off center in portrait.
I have played around with some of the contentMode settings but not had any luck, I either get portrait to be center or landscape to be center but not both.
Could someone please help me with getting the UIImageView centered in both portrait and landscape after rotation.
Thanks in advance
Matt

You need to set the contentMode, the frame and an appropriate autoresizing mask:
UIImageView *imgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"background-image"]];
imgView.frame = self.view.bounds;
imgView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
imgView.contentMode = UIViewContentModeScaleAspectFill;
[self.view addSubview:imgView];
[self.view sendSubviewToBack:imgView];
[imgView release];

Check the autoresizingMask property for the imageView.

Related

How to set UIImageView frame in ViewController for Universal App?

For a Universal App, how do I set the UIImageView frame in the ViewController?
UIView *baseView = [[UIView alloc] init];
[self.view addSubview:baseView];
// Displays UIImageView
UIImageView *myImage = [[UIImageView alloc]
initWithImage:[UIImage imageNamed:#"tool.png"]];
[baseView addSubview:myImage];
When I run app it shows black screen. So i guess I have to set a frame for it.
You kinda answered your own question - yes you have to set a frame for it.
You may want to try:
UIView *baseView = [[UIView alloc] initWithFrame:self.view.bounds];
[self.view addSubview:baseView];
// Displays UIImageView
UIImageView *myImage = [[UIImageView alloc]
initWithImage:[UIImage imageNamed:#"tool.png"]];
myImage.frame = baseView.bounds;
[baseView addSubview:myImage];
You could also set the autoresizingmask for baseView and myImage so that as self.view's frame changes, so do these two views.
Also, just in case you aren't using ARC - don't forget to autorelease these views before leaving this method.

Make a rectangle equal to image view

I have an imageView. I need to restrict the size of imageView to the white background imageOnly.The image is tightly bounded between right,left and top margin. I am confused on how to do at bottom, such that the image must fit in the white background only. How do I do that ?
http://i.stack.imgur.com/ztvQe.png
The code I am using
_imageView = [[UIImageView alloc] initWithFrame:CGRectMake(25, 240,175,205)];
Frame width and height is 205*205
You can add your imageView as a subview of the view with the white background.
UIView *whiteBackgroundView = [[UIView alloc] initWithFrame:CGRectMake(25,240,205,205)];
CGSize whiteSize = whiteBackgroundView.frame.size;
_imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0,size.width,size.height)];
[whiteBackgroundView addSubview:_imageView];
In the example your imageView will completely fit the size of the white background view.
Try this
UIView *productView = [[UIView alloc]initWithFrame:CGRectMake(25, 240,225,225)];
_imageView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 10,205,205)];
_imageView.clipsToBounds =YES;
[productView addSubview:_imageView];
[self.view addSubview:ImageBackview];
just create one view and add this image in it, for example use bellow code..
UIView *ImageBackview = [[UIView alloc]initWithFrame:CGRectMake(25, 240,225,225)];
_imageView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 10,205,205)];
[ImageBackview addSubview:_imageView];
[self.view addSubview:ImageBackview];

How to reset the background of a UIView

After adding a background (image) to my UIView (View_0) I wish to implement a button to remove the same background.
How can I do it? I have tried to set a different image as the background of View_0 but this does not work (I suspect this background is set behind "image").
I don't want to use
[View_0 removeFromSuperview];
as I need View_0 to still be there for other purposes....
//Create an ImageView
UIImageView *image = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 940, 422)];;
//Add the ImageView as the background of the View_0
[View_0 addSubview:image];
//Move custom image behind View
[View_0 sendSubviewToBack: image];
//Failed attempt to remove the background....
View_0.backgroundColor = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:#"defaultImage.PNG"]];
You have added a UIImageView to the View and Not the background of the view. So what is happening is that when u implement
View_0.backgroundColor = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:#"defaultImage.PNG"]];
The background of the View is being set but the UIImageView is now on top of the View so that is why the background of the View is not being shown.
Instead if you simply want to add the background you can change it to the Action of the UIButton.
Also if you use [View_0 removeFromSuperview]; you are trying to remove the view which is not right.
//Create an ImageView
UIImageView *image = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 940, 422)];;
image.tag = 1234;
//Add the ImageView as the background of the View_0
[View_0 addSubview:image];
//Move custom image behind View
[View_0 sendSubviewToBack: image];
UIImageView *imgView = [self.view viewWithTag:1234];
[img setImage:nil];
This should work i think

UITableViewController background image stretched

I'm trying to set the background image for a UITableViewController. I've got the following code which does set the background fine, but the image itself is being stretched to about twice the usual size.
[[self view] setAutoresizesSubviews:NO];
UIView *backgroundView = [[UIView alloc] initWithFrame:self.view.frame];
backgroundView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"Default.png"]];
self.tableView.backgroundView = backgroundView;
self.tableView.backgroundColor = [UIColor clearColor];
[backgroundView release];
The image is only 640x960 pixels.
I've tried manually setting the frame of the backgroundView but it doesn't change the size of the background image.
Can anyone explain where I'm going wrong?
Many thanks.
In my expirience, you should avoid using
[UIColor colorWithPatternImage: ... ];
as it leads to some problems with picking the right image for Retina/Low-Res devices.
Try something more like this:
UIImageView* bgView = [[[UIImageView alloc] initWithImage:
[UIImage imageNamed:#"Default.png"]] autorelease];
[tableView.backgroundView addSubview:bgView];
Also make sure you have two PNGs:
Default.png - 320 x 480
Default#2x.png = 640 x 960

Image displayed using CGRect is obscuring UIButtons

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];