1

I have a high resolution image and I want to use it as a background for a view. But when I add it either via an Interface Builder or programatically I see only its part.This doesn't help:

UIImage* _backGround = [UIImage imageNamed:@"background-clean@2x.png"];
  CGRect _viewFrame = self.view.frame;
 [_backGround drawInRect:_viewFrame];
UIImageView* _backGroundView = [[UIImageView alloc] initWithImage:_backGround];
[self.view addSubview:_backGroundView];
[self.view sendSubviewToBack:_backGroundView];

And this too:

_backGroundView.contentMode =UIViewContentModeScaleAspectFit;

So the question how can I scale this image in order it fits in the view in a full size in spite of its size? P.S. Sorry for my bad English.

MainstreamDeveloper00
  • 8,436
  • 15
  • 56
  • 102
  • http://stackoverflow.com/questions/185652/how-to-scale-a-uiimageview-proportionally – Gereon Feb 03 '13 at 13:28
  • what is the name of the image? – holex Feb 03 '13 at 13:39
  • @holex Does this matter?"background.png" – MainstreamDeveloper00 Feb 03 '13 at 13:55
  • @Gereon This doesn't help.I see only part of the image – MainstreamDeveloper00 Feb 03 '13 at 13:55
  • @HarryCater, **EDITED:** of course it is matter! if the image resolution is for retina screen you have to rename the image to `background@2x.png` for showing the image on the retina or normal screen correctly. – holex Feb 03 '13 at 13:59
  • @HarryCater, you can read more here the **[App-Related Resources](http://developer.apple.com/library/ios/#documentation/iphone/conceptual/iphoneosprogrammingguide/App-RelatedResources/App-RelatedResources.html)** of the official developers' documentations. – holex Feb 03 '13 at 14:07
  • Please explain what "its part" means: how large (in pixels) is background-clean.png, and what is the size of the view? Maybe what you're looking for is image resizing like it's shown here: http://vocaro.com/trevor/blog/2009/10/12/resize-a-uiimage-the-right-way/ – Gereon Feb 03 '13 at 15:46

3 Answers3

2

I agree with Ali, when creating retina-sized images you should also scale them and add their smaller version to your project. With both versions you can simply specify the full name of the image without the @2x.png or .png extension.

But, to fix your implementation you only need one extra line of code:

UIImage* _backGround = [UIImage imageNamed:@"background-clean"];
UIImageView* _backGroundView = [[UIImageView alloc] initWithImage:_backGround];

_backGroundView.frame = self.view.frame;

[self.view addSubview:_backGroundView];
[self.view sendSubviewToBack:_backGroundView];

Once you change the frame of the UIImageView, its contents (i.e. your image) will scale as well.

C4 - Travis
  • 4,502
  • 4
  • 31
  • 56
0

You must not include @2x in your code when referencing the name of the image!

You should:

  • Include both files Background.png and Background@2x.png in your Xcode projet resources, Background@2x.png being twice the size (in pixels) of Background.png
  • But always refer to this file (for example in imageNamed:) as @"Background.png" or even just @"Background": [UIImage imageNamed:@"Background"]

Then iOS will do its magic all by itself, choosing the right image between Background.png or Background@2x.png depending on if the device screen is Retina or not.

For more info see here in Apple documentation.

AliSoftware
  • 32,623
  • 6
  • 82
  • 77
0
[self.view setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"background-clean"]]];
Lilo
  • 2,724
  • 25
  • 16