Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to tile images in a UIScrollView without having to manually create all the tiles?

In the iPhone sample code "PhotoScroller" from WWDC 2010, they show how to do a pretty good mimmic of the Photos app with scrolling, zooming, and paging of images. They also tile the images to show how to display high resolution images and maintain good performance.

Tiling is implemented in the sample code by grabbing pre scaled and cut images for different resolutions and placing them in the grid which makes up the entire image.

My question is: is there a way to tile images without having to manually go through all your photos and create "tiles"? How is it the Photos app is able to display large images on the fly?

Edit Here is the code from Deepa's answer below:

- (UIImage *)tileForScale:(float)scale row:(int)row col:(int)col size:(CGSize)tileSize  image:(UIImage *)inImage
{
CGRect subRect = CGRectMake(col*tileSize.width, row * tileSize.height, tileSize.width, tileSize.height);
CGImageRef tiledImage = CGImageCreateWithImageInRect([inImage CGImage], subRect);
UIImage *tileImage = [UIImage imageWithCGImage:tiledImage]; 
return tileImage;
}
like image 735
Jonah Avatar asked Jul 26 '10 18:07

Jonah


1 Answers

Here goes the piece of code for tiled image generation:

In PhotoScroller source code replace tileForScale: row:col: with the following:

inImage - Image that you want to create tiles

- (UIImage *)tileForScale: (float)scale row: (int)row column: (int)col size: (CGSize)tileSize image: (UIImage*)inImage
{
    CGRect subRect = CGRectMake(col*tileSize.width, row * tileSize.height, tileSize.width, tileSize.height);
    CGImageRef tiledImage = CGImageCreateWithImageInRect([inImage CGImage], subRect);
    UIImage *tileImage = [UIImage imageWithCGImage: tiledImage];
    return tileImage;
}

Regards, Deepa

like image 124
spd Avatar answered Oct 04 '22 04:10

spd