oxygen
oxygen

Reputation: 1

UIView image background

In my application, I want to set the background image on UIView. In the simulator iOS this code works. And on the iPhone black background. Here's the code:

    - (void)viewDidLoad
{
    [super viewDidLoad];
    UIGraphicsBeginImageContext(self.view.frame.size);
    [[UIImage imageNamed:@"appBG.png"] drawInRect:self.view.bounds];
    UIImage *appbg = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    self.view.backgroundColor = [UIColor colorWithPatternImage:appbg];
}

UPDATE: I decided my problem

Upvotes: 0

Views: 869

Answers (3)

Dipak Chaudhari
Dipak Chaudhari

Reputation: 655

Try with Replace following line:

[[UIImage imageNamed:@"appBG.png"] drawInRect:self.view.bounds];

With following line:

[[UIImage imageNamed:@"appBG.png"] drawInRect:self.view.frame];

May be the issue of frame or bound.

Upvotes: 0

Rahul
Rahul

Reputation: 134

its better u will take an ImageView on UIView and set the image to it. Rather than setting the image to UIView.

Upvotes: 2

Mrunal
Mrunal

Reputation: 14118

Try this code:

UIImage*    backgroundImage = [UIImage imageNamed:@"appBG.png"];
CALayer*    aLayer = [CALayer layer];
CGFloat nativeWidth = CGImageGetWidth(backgroundImage.CGImage);
CGFloat nativeHeight = CGImageGetHeight(backgroundImage.CGImage);
CGRect      startFrame = CGRectMake(0.0, 0.0, nativeWidth, nativeHeight);
aLayer.contents = (id)backgroundImage.CGImage;
aLayer.frame = startFrame;
[self.view.layer addSublayer:aLayer];
[aLayer setNeedsDisplay];

Adjust startFrame value according to your requirement.

Upvotes: 0

Related Questions