Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Background Image Spanning Across Multiple UITableViewCells (Grouped)

I'm using a grouped tableview with some custom cells, and an image as each cell's backgroundColor.

cell.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"custombackground.png"]];

So far, all is good, but my image is significantly taller than the height of a regular cell, and therefore, only the top part of the image is being used as the background, and each cell is identical. Basically, it looks sloppy with each cell repeating the pattern.

Has anyone done this so the second row of the section displays the next part of the image instead of repeating it, so it looks like a single continuous image? (e.g. row 0 uses the first 44 pts of the image, row 1 uses pts 45-88, etc.)

Is it possible, and/or any ideas how to?

like image 632
gyratory circus Avatar asked Jun 15 '26 07:06

gyratory circus


1 Answers

Yes, you can do this. The key to this problem is using UIImage's CGImage method, and then using Quartz "CGImageRef CGImageCreateWithImageInRect();" to create sub images. With a CGImageRef you can create new UIImages.

So lets say its just the height that is the problem. The first cell uses a partial image from the top down "h" pixels. Then the next starts at "h" and goes another h pixels, etc.

Note that Quartz uses a "flipped" coordinate system, so you will have to fiddle with exactly where you grab the sub image from. When I do this I always seem to need some tweaking, so expect to spend a bit of time at the end getting it to show properly.

like image 130
David H Avatar answered Jun 18 '26 00:06

David H



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!