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.
Have a try.
self.layer.contents = (id)[UIImage imageNamed:@"freshBackground.png"].CGImage;
self.view.layer.contents = UIImage(named: "freshBackground")!.cgImage
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]];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With