Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ios: colorWithPatternImage , stretch image full screen

I have one view , and want set backGroudColor for this view by UIImage. The code as :

self.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"backImage.png"]]; 

the problem is: the backImage'frame is small than the view. how to stretch UIImage to full my view. I konw use UIImageView can reach.

someone have good idea?

update:

I can't upload one image.

Like this:backImge's size is 30*30, and my view's size is 1024*700, when i use myview.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"backImage.png"]];

The result is myview's backGroup has many 'backImage.png'. my goal is have one 'backImage.png' streth full myview.

like image 827
lee_zhou Avatar asked May 17 '12 03:05

lee_zhou


2 Answers

Have a try.

self.layer.contents = (id)[UIImage imageNamed:@"freshBackground.png"].CGImage;

Swift:

self.view.layer.contents = UIImage(named: "freshBackground")!.cgImage 
like image 145
world000 Avatar answered Sep 21 '22 22:09

world000


As far as I know, you cannot resize the background's pattern image directly. The easiest way is just to change your image to fit your parent view's frame size. Or you can redraw your image to fit the size of your parent view.

UIView * yourView = [[UIView alloc] initWithFrame:CGRectMake(10.f, 100.f, 300.f, 100.f)]; UIImage * targetImage = [UIImage imageNamed:@"backImage.png"];  // redraw the image to fit |yourView|'s size UIGraphicsBeginImageContextWithOptions(yourView.frame.size, NO, 0.f); [targetImage drawInRect:CGRectMake(0.f, 0.f, yourView.frame.size.width, yourView.frame.size.height)]; UIImage * resultImage = UIGraphicsGetImageFromCurrentImageContext();     UIGraphicsEndImageContext();  [yourView setBackgroundColor:[UIColor colorWithPatternImage:resultImage]]; 
like image 29
Kjuly Avatar answered Sep 21 '22 22:09

Kjuly