42

I want to have a UIImageas a background image on a UIView.

Here is my code:

UIColor *background = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:@"login.png"]];
self.view.backgroundColor = background;
[background release];

The Problem is, that the scale is not okay. It scaled 640 to 480, so I can't tell it 100% sure but it looks like only 300 to 250 is displayed or something like that.

Is there a fit to scale / fit to UIView / fit to size modus?

Hossam Ghareeb
  • 7,063
  • 3
  • 53
  • 64
PassionateDeveloper
  • 14,558
  • 34
  • 107
  • 176

13 Answers13

54

you are required to process your image before you add it, try this :

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

self.view.backgroundColor = [UIColor colorWithPatternImage:image];
uneakharsh
  • 740
  • 6
  • 7
  • In case you got the image flipped, you have to change the context orientation with CGContextTranslateCTM() – geezmo Jan 12 '16 at 18:37
29

hello try this,

     self.view.backgroundColor=[UIColor colorWithPatternImage:[UIImage imageNamed:@"login.png"]];
Dipak Chaudhari
  • 655
  • 4
  • 8
12

Specify an actual background and send it to the back of your view

//add background
UIImageView *background = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"bg.png"]];
[myView addSubview:background];
[myView sendSubviewToBack:background];
myView.contentMode = UIViewContentModeScaleAspectFit;
10

In Swift use this...

    UIGraphicsBeginImageContext(self.view.frame.size)
    UIImage(named: "1.png")?.drawInRect(self.view.bounds)

    var image: UIImage = UIGraphicsGetImageFromCurrentImageContext()

    UIGraphicsEndImageContext()

    self.view.backgroundColor = UIColor(patternImage: image)
Mohammad Zaid Pathan
  • 16,304
  • 7
  • 99
  • 130
9

And if you want to do it in swift:

self.view.backgroundColor = UIColor(patternImage: UIImage(named:"background.jpg"))
Kevin Delord
  • 2,498
  • 24
  • 22
9

According to the UIColor API:

You can use pattern colors to set the fill or stroke color just as you would a solid color. During drawing, the image in the pattern color is tiled as necessary to cover the given area.

That means it tries to create a pattern out of your image to fill the area. I think the optimal way to do it is so rescale your image to fit exactly the UIView size.

There is a nice answer here how to resize your image during runtime:

The simplest way to resize an UIImage?

Community
  • 1
  • 1
GooKSL
  • 387
  • 2
  • 14
2

I'm using the following extension in Swift:

extension UIView{

    func setBackgroundImage(img: UIImage){

        UIGraphicsBeginImageContext(self.frame.size)
        img.drawInRect(self.bounds)
        let patternImage: UIImage = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        self.backgroundColor = UIColor(patternImage: patternImage)
    }
}

In code you can use like this:

if let image = UIImage(named: "HomeBg"){
        self.view.setBackgroundImage(image)
    }
Hossam Ghareeb
  • 7,063
  • 3
  • 53
  • 64
1

In the Interface Builder, add an Image View to View and select the image you want to use in Attributes inspector. Finally, make the Image View the very first child of the View by dragging the element in Document Outline.

Dino Tw
  • 3,167
  • 4
  • 34
  • 48
1

If the view is going to have the same size, you can place a method in your AppDelegate.m that scales the background image on first run, and then keeps the scaled image in memory for future use:

UIImage *backgroundImage = nil;
- (void)setBackground:(UIView *)view
{
    if (backgroundImage == nil)
    {
        UIGraphicsBeginImageContext(view.frame.size);
        [[UIImage imageNamed:@"Background"] drawInRect:view.bounds];
        backgroundImage = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
    }
    view.backgroundColor = [UIColor colorWithPatternImage:backgroundImage];
}

Thanks to @uneakharsh for his help!

Zorayr
  • 23,770
  • 8
  • 136
  • 129
1

I also wanted to do this and I found that, due to autolayout, the UIImageView stretches the containing UIView, when I want it the other way: I want the UIImageView to take the size of the UIView.

So I created this class. It updates the UIImageView frame according to the UIView container whenever it is laid out via layoutSubviews.

/**
 * View with an image whose frame is adjusted to the frame of the view. 
 */
class ViewWithImage: UIView {

    let image: UIImageView

    init(image: UIImageView) {
        self.image = image
        super.init(frame: .zero)
        self.addSubview(image)
    }

    override func layoutSubviews() {
        super.layoutSubviews()
        updateImageFrame()
    }

    private func updateImageFrame() {
        image.frame = CGRect(origin: .zero, size: self.frame.size)
    }

    required init?(coder: NSCoder) { fatalError("no init(coder:)") }
}
Ferran Maylinch
  • 10,919
  • 16
  • 85
  • 100
0
UIColor *background = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:@"image.png"]];
self.view.backgroundColor = background;
[background release];
Aziz Shaikh
  • 16,245
  • 11
  • 62
  • 79
Manu P
  • 1
0

You can use UIGraphicsBeginImageContext method to set the size of image same that of view.

Syntax : void UIGraphicsBeginImageContext(CGSize size);

  #define IMAGE(imageName) (UIImage *)[UIImage imageWithContentsOfFile:
[[NSBundle mainBundle] pathForResource:imageName ofType:IMAGE_TYPE_PNG]]

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

    self.view.backgroundColor = [UIColor colorWithPatternImage:IMAGE(@"mainBg")];
Jayprakash Dubey
  • 35,723
  • 18
  • 170
  • 177
-1
this.View.BackgroundColor = UIColor.FromPatternImage (UIImage.FromFile ("body_background.png"));
Baby Groot
  • 4,637
  • 39
  • 52
  • 71
user3098847
  • 309
  • 3
  • 2