0

i am setting my view's background as an image. for that using the following code. But the image seems to be getting stretched. How can i get the original image as it is?

self.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"image.png"]];
Kampai
  • 22,848
  • 21
  • 95
  • 95
Mr.B
  • 70
  • 8
  • 1
    What is the resolution for an image ? .It should be either 320*480 or 320 * 568 for 3.5 & 4 inch screen phone – poojathorat Nov 26 '14 at 10:10
  • 2
    possible duplicate of [How to fill background image of an UIView](http://stackoverflow.com/questions/8077740/how-to-fill-background-image-of-an-uiview) – n00bProgrammer Nov 26 '14 at 10:18

2 Answers2

3

This is not advisable to set UIView background colour (to set an image) like this, instead add an UIImageView as subview of UIView and then assign image it to it. This will be same look like you've setting a background.

However, here's the solution for this,

UIGraphicsBeginImageContext(self.view.frame.size);
[[UIImage imageNamed:@"image.png"] drawInRect:self.view.bounds];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

self.view.backgroundColor = [UIColor colorWithPatternImage:image];

Straight from this answer

Community
  • 1
  • 1
Hemang
  • 26,840
  • 19
  • 119
  • 186
0
UIGraphicsBeginImageContext(self.view.frame.size);
[[UIImage imageNamed:@"myImage.png"] drawInRect:self.view.bounds];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
self.view.backgroundColor = [UIColor colorWithPatternImage:image];

Try this code.It worked for me.

Nikita Khandelwal
  • 1,741
  • 16
  • 25