1

Is there any solution on creating tiles of photos "on the fly" with a good performance?! I've already looked here, but this is not so performant.

The aim is to create a fast photo viewer like Apples photoapp. Therefor I need to create the tiles from photos which are created from or downloaded to the app.

Community
  • 1
  • 1
cweinberger
  • 3,518
  • 1
  • 16
  • 31

1 Answers1

2

Your best bet is probably to use Core Graphics and ImageI/O

CGImageRef image = ...; // the big image to slice, don't draw with this image or it will decompress and might chew up all your memory
NSURL *url = ...; // the url for this slice
NSString *uti = @"public.jpeg"; // just an example, you should get the correct one for your images
CGSize sliceSize = 256.0; // for example
size_t column = 0;
size_t row = 0; // row and column should vary across your image
CGFloat colWidth = 256.0; // should account for the final column not being 256
CGFloat rowWidth = 256.0; // should account for the final row not being 256
CGRect sliceRect = CGRectMake(floor(column * sliceSize), floor(row * sliceSize),
                                    floor(colWidth), floor(rowHeight));
CGImageRef slicedImage = CGImageCreateWithImageInRect(image, sliceRect);
if(NULL == slicedImage) {
    NSLog(@"hosed image");
}
CGImageDestinationRef destination = CGImageDestinationCreateWithURL((CFURLRef)url, (CFStringRef)uti, 1, NULL);
CGImageDestinationAddImage(destination, slicedImage, NULL);
if(!CGImageDestinationFinalize(destination)) {
    NSLog(@"hosed write of %@", [url path]);
}
CFRelease(destination);
CGImageRelease(slicedImage);

As a word of caution, this is cobbled together from memory and is probably wrong. Please read the docs to see where I messed up.

Here are the docs about this technique.

http://developer.apple.com/library/ios/documentation/GraphicsImaging/Reference/CGImage/Reference/reference.html#//apple_ref/c/func/CGImageCreateWithImageInRect

http://developer.apple.com/library/ios/#documentation/GraphicsImaging/Conceptual/ImageIOGuide/imageio_basics/ikpg_basics.html

Bill Dudney
  • 3,358
  • 1
  • 16
  • 13
  • Unfortunalety it's not that performant. I guess it's not possible to create tiles "on the fly" in an acceptable time. but thanks for your answer! – cweinberger May 10 '11 at 10:11