Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stretchable and Pattern UIImage at the same time

I would like to code a shelf and I don't want to think about resizing and changing background images on a rotation and depending on the screensize (iPad/iPhone). Is it possible to create an image, that would stretch horizontally but repeat vertically? By now, I've only found the [UIColor colorWithPatternImage:] and [UIImage strechableImageWithLeftCapWidth:topCapHeight:] or the new [UIImage resizableImageWithCapInsets:]. But I didn't manage to let them work together, obviously.

I hope the illustration helps understanding my issue:

Illustration

Do you have any idea how to accomplish the above, so that it will work with a single image for different sizes and orientations? Thanks!

like image 923
denbec Avatar asked Nov 15 '11 18:11

denbec


1 Answers

I also needed to set UIView background from image and change its size at the same time. What I did was:

  1. Created image within frame of my view size
  2. Set view background using [UIColor colorWithPatternImage:(UIImage*)]

And here is the code :

UIGraphicsBeginImageContext(self.followView.frame.size);
[[UIImage imageNamed:@"background_content.png"] drawInRect:self.followView.bounds];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UIColor *bgColor = [UIColor colorWithPatternImage:image];

[followView setBackgroundColor:bgColor];
like image 111
morgan_il Avatar answered Sep 29 '22 02:09

morgan_il