Dhanaraj
Dhanaraj

Reputation: 1017

CGImage: create thumbnail image with desired size

I want to create a thumbnail image using Core Graphics. The thumbnail should be size 1024 [sic] with the same aspect ratio as the original image. Is it possible to set the desired size for the thumbnail directly in Core Graphics?

In the options dictionary below, I can pass the max size of the thumbnail to be created, but is there any way to pass a minimum size?

 NSURL * url = [NSURL fileURLWithPath:inPath];
 CGImageSourceRef source = CGImageSourceCreateWithURL((CFURLRef)url, NULL);
 CGImageRef image=nil;
 if (source)
 {
  NSDictionary* thumbOpts = [NSDictionary dictionaryWithObjectsAndKeys:
           (id) kCFBooleanTrue, (id)kCGImageSourceCreateThumbnailWithTransform, 
           (id)kCFBooleanTrue, (id)kCGImageSourceCreateThumbnailFromImageIfAbsent,
           [NSNumber numberWithInt:2048],  kCGImageSourceThumbnailMaxPixelSize,
           
           nil];
  
  image = CGImageSourceCreateThumbnailAtIndex(source, 0, (CFDictionaryRef)thumbOpts);   
  
  NSLog(@"image width = %d %d", CGImageGetWidth(image), CGImageGetHeight(image));
  CFRelease(source);
 }

Upvotes: 10

Views: 8290

Answers (2)

Aleks N.
Aleks N.

Reputation: 6247

Swift 3 version of the answer:

func loadImage(at url: URL, maxDimension max: Int) -> UIImage? {
    
    guard let imageSource = CGImageSourceCreateWithURL(url as CFURL, nil)
        else {
            return nil
    }

    let options: [CFString: Any] = [
        kCGImageSourceShouldAllowFloat: true,
        kCGImageSourceCreateThumbnailWithTransform: true,
        kCGImageSourceCreateThumbnailFromImageAlways: true,
        kCGImageSourceThumbnailMaxPixelSize: max
    ]
    
    guard let thumbnail = CGImageSourceCreateThumbnailAtIndex(imageSource, 0, options as CFDictionary)
        else {
            return nil
    }
    
    return UIImage(cgImage: thumbnail)
}

Upvotes: 4

matt
matt

Reputation: 534885

If you want a thumbnail with size 1024 (maximum dimension), you should be passing 1024, not 2048. Also, if you want to make sure the thumbnail is created to your specifications, you should be asking for kCGImageSourceCreateThumbnailFromImageAlways, not kCGImageSourceCreateThumbnailFromImageIfAbsent, since the latter might cause an existing thumbnail to be used, and it could be smaller than you want.

So, here's code that does what you ask:

NSURL* url = // whatever;
NSDictionary* d = [NSDictionary dictionaryWithObjectsAndKeys:
                   (id)kCFBooleanTrue, kCGImageSourceShouldAllowFloat,
                   (id)kCFBooleanTrue, kCGImageSourceCreateThumbnailWithTransform,
                   (id)kCFBooleanTrue, kCGImageSourceCreateThumbnailFromImageAlways,
                   [NSNumber numberWithInt:1024], kCGImageSourceThumbnailMaxPixelSize,
                   nil];
CGImageSourceRef src = CGImageSourceCreateWithURL((CFURLRef)url, NULL);
CGImageRef imref = CGImageSourceCreateThumbnailAtIndex(src, 0, (CFDictionaryRef)d);
// memory management omitted

Upvotes: 21

Related Questions