Setting retina background images appear zoomed in - iphone

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

Related

Why is this image not being added to my scrollview?

Why is this image not being added to my UIScrollView? A white screen is being displayed:
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(self.scrollView.frame.size.width * 1, 0, self.scrollView.frame.size.width, self.scrollView.frame.size.height)];
imageView.backgroundColor = [UIColor redColor];
[imageView setImage:[UIImage imageNamed:#"placeholder.png"]];
[self.scrollView addSubview:imageView];
self.scrollView.contentSize = CGSizeMake(self.scrollView.frame.size.width * self.urlArray.count, self.scrollView.frame.size.height);
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.scrollView.frame.size.width, self.scrollView.frame.size.height)];
Ans also give proper contentSize of UIScrollView and add UIScrollView to your UIView

animated gif on uibutton

I use this code, that work, really size of image 160x148 but image button is view very big on all screen!
UIImageView* animatedImageView = [[UIImageView alloc] initWithFrame:self.view.bounds];
animatedImageView.animationImages = [NSArray arrayWithObjects:
[UIImage imageNamed:#"button1.png"],
[UIImage imageNamed:#"button2.png"],
[UIImage imageNamed:#"button3.png"],
[UIImage imageNamed:#"button4.png"], nil];
animatedImageView.animationDuration = 1.0f;
animatedImageView.animationRepeatCount = 0;
[animatedImageView startAnimating];
[yourButton addSubview: animatedImageView];
How fix that?
The images are big because you called initWithFrame: and passed in your view's bounds. Change it to a different-sized rect, such as:
CGRectMake(0, 0, 160, 148)
... and then reposition it as you see fit.
Try this code.It will help you for correct solution.
UIImage *myimage=UIImage imageNamed:#"button1.png";
Button.imageview.image=myimage;

Image inside Scrollview in iPhone

I am new to iPhone,
I want to do scrolling in my image which is inside scrollView but i am unable to scroll my image,
Here is my code snippet,
UIImage *image1 = [UIImage imageWithContentsOfFile:#"768bg_with_footer.gif"];
self.imageView = [[UIImageView alloc] initWithImage:image1];
[self.scrollView addSubview:self.imageView];
scrollView.contentSize = image1.size;
self.scrollView.contentOffset = CGPointZero;
I have taken ScrollView in my .xib file.
Any help will be appreciated.
Try this code:
UIImage *image1 = [UIImage imageNamed:#"768bg_with_footer.gif"];
self.imageView = [[UIImageView alloc] initWithImage:image1];
[self.scrollView addSubview:self.imageView];
scrollView.contentSize = image1.size;
self.scrollView.contentOffset = CGPointZero;
We use initWithContentsOfFile when we know the path of the image.Using it with imageName will give a null image.
try following instead
self.scrollView.contentSize = self.imageView.frame.size;

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 set UIImage frame

How i can set the frame size of UIImage.
You can set the frame of the UIImageView:
UIImageView* imgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
imgView.image = [UIImage imageNamed:#"image.png"];
(and don't forget to [imgView release]; somewhere)
UIImage has no frame. It has only width and height. If you want to put it on your view you should create UIImageView
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(12,12,123,123)];
imageView.image = image; // image - your UIImage