Using imageWithData:scale: in setThumbnailDataFromImage:


I realize that our textbook was published prior to iOS 6.0 coming out. However, in following the authors’ guidance to make it a habit to read the API doc, I learned that in iOS 6.0 the UIImage method imageWithData:scale: became available. Hence, I implemented the functionality (albeit not the logic) described in pages 306-307 of the (paper) book this way:

- (void)setThumbnailDataFromImage:(UIImage *)image {
    CGSize imageSize = [image size];
    // Figure out a scaling ratio, and make sure to maintain the same aspect ratio
    // (the thumbnail imageView is sized 40x40)
    float scale = MAX(40.0 / imageSize.height, 40.0 / imageSize.width);

    // If the image size happens to be smaller than the thumbnail, do not scale it
    if (scale > 1.0) {
        scale = 1.0;
    NSData *data = UIImagePNGRepresentation(image);
    [self setThumbnail:[UIImage imageWithData:data scale:scale]];
    [self setThumbnailData:UIImagePNGRepresentation(thumbnail)];

Upon testing this on both the iPhone and iPad, the desired effect seems to be happening.

Just sharing with my fellow road warriors. Again, I realize that this will only work for apps targeted for iOS 6.x.