Thumbnail view of images

iphone, objective-c

Solution

Are you asking how to create thumbnails from larger images, or about how to build a view controller which displays them nicely?

Building a view controller:

You could use `TTPhotoViewController` from the Three20 project (description), which acts similarly to the iPhone's built in camera roll to view images.

You can look at the `Scrolling` sample code from apple, referred to in this question about using it for thumbnails.

If you want to build one yourself, you might consider using a `GridView` from the moriarty library, either as a large grid in a `UIScrollView`, or on a more efficient row-by-row basis in a `UITableView`. There's a previous question on optimized image loading in a `UIScrollView`.

Creating thumbnails from larger images:

The code-easiest way to scale down an image is to simply display it in a `UIImageView` with a frame set to the smaller size that you want - it's scaled for you.

If you want to save a thumbnail, or care about memory usage, you can create a hard-scaled version. The sample code below is taken from this blog post, and can be added to `UIImage` as a category method:

- (UIImage*) imageScaledToSize: (CGSize) newSize {
  UIGraphicsBeginImageContext(newSize);
  [self drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];
  UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
  UIGraphicsEndImageContext();

  return newImage;
}

Finally, here's a previous question on masking out round corners on an image, similar to the app icons used on the home screen.

Added

Using an image's built-in thumbnail:

There's a nice function called `CGImageSourceCreateThumbnailAtIndex` that understands built-in thumbnails in certain image data formats. You can see some useful sample code for this under Creating a Thumbnail Image from an Image Source in the Image I/O Programming Guide from Apple.

Problem

I want to show some images as a thumbnail in View controller but i dont know how to do this. Can anyone please give me some ideas or sample code if possible.

Original source

Related problems