6

I want to use the function CGImageSourceCreateThumbnailAtIndex to create a thumbnail from an UIImage. All I have is the UIImage itself. The image is the snapshot on a UIView.

Please, I don't want to use any other method to create the thumbnail, just the one using CGImageSourceCreateThumbnailAtIndex, because I want to compare its performance with the other methods I already have.

Said that, this is the code I have so far.

I have create a UIImage category with this code:

- (UIImage *)createSquaredThumbnailWithWidth:(NSInteger)width {

  CFDictionaryRef options = (__bridge CFDictionaryRef) @{
                                                         (id) kCGImageSourceCreateThumbnailWithTransform : @YES,
                                                         (id) kCGImageSourceCreateThumbnailFromImageAlways : @YES,
                                                         (id) kCGImageSourceThumbnailMaxPixelSize : @(width)
                                                         };

  CGImageRef scaledImageRef = CGImageSourceCreateThumbnailAtIndex(????, 0, options);


  UIImage* scaled = [UIImage imageWithCGImage:scaledImageRef];

  CGImageRelease(scaledImageRef);

  return scaled;
}

My problem is with this line:

  CGImageRef scaledImageRef = CGImageSourceCreateThumbnailAtIndex(????, 0, options);

The first parameter of this function demands a CGImageSourceRef, but like I said, I just have an UIImage, that image is on memory, not on disk, and I don't want to save it to disk, or the performance will go down the drain.

How do I get a CGImageSourceRef from a UIImage that is on memory???

2
  • what about on memory? all image instances are in memory. Use it as @Nirav D said. Nov 18, 2016 at 12:24
  • 1
    Out of curiosity, which method for creating a thumbnail from a UIImage is the 'best' (in terms of memory usage, time etc)?
    – Z S
    Nov 14, 2018 at 19:55

4 Answers 4

23

Swift 4 code

let imageData = UIImagePNGRepresentation(image)!
let options = [
    kCGImageSourceCreateThumbnailWithTransform: true,
    kCGImageSourceCreateThumbnailFromImageAlways: true,
    kCGImageSourceThumbnailMaxPixelSize: 300] as CFDictionary
let source = CGImageSourceCreateWithData(imageData, nil)!
let imageReference = CGImageSourceCreateThumbnailAtIndex(source, 0, options)!
let thumbnail = UIImage(cgImage: imageReference)
4
  • This really came handy after series of web search! Thanks for this wonderful solution.
    – Aweda
    Mar 5, 2018 at 10:19
  • 1
    Also review a post nshipster.com/image-resizing by mattt where he has listed many image scaling techniques and their performance benchmarks.
    – Vijayendra
    Jul 8, 2020 at 14:03
  • How to make thumbnail image orientation not rotated, i tried by changing kCGImageSourceCreateThumbnailWithTransform to false, but still it is rotating, All i need is whether original image is of portrait or landscape , i dont need thumbnail to be rotated.
    – virtplay
    Aug 21, 2020 at 14:43
  • @virtplay you can apply image rotation from original image: UIImage(cgImage: cgImage, scale: originalImage.scale, orientation: originalImage.imageOrientation)
    – snake302
    Jul 10 at 11:29
14

Swift 5.1 extension

based on Ivan's answer

extension UIImage {

  func getThumbnail() -> UIImage? {

    guard let imageData = self.pngData() else { return nil }

    let options = [
        kCGImageSourceCreateThumbnailWithTransform: true,
        kCGImageSourceCreateThumbnailFromImageAlways: true,
        kCGImageSourceThumbnailMaxPixelSize: 300] as CFDictionary

    guard let source = CGImageSourceCreateWithData(imageData as CFData, nil) else { return nil }
    guard let imageReference = CGImageSourceCreateThumbnailAtIndex(source, 0, options) else { return nil }

    return UIImage(cgImage: imageReference)

  }
}
1
  • 1
    .jpegData(compressionQuality: 0.95) is faster than .pngData() while providing almost the same quality Apr 21, 2022 at 17:25
12

Have you try using CGImageSourceCreateWithData and passing image data as CFDataRef like this:

Note: Using CGImageSourceCreateWithData loses information about image rotation, use the CGImageSourceCreateWithURL for hassle free rotation of final image.

NSData *imageData = UIImagePNGRepresentation(image);
CGImageSourceRef src = CGImageSourceCreateWithData((__bridge CFDataRef)imageData, NULL);
CFDictionaryRef options = (__bridge CFDictionaryRef) @{
                                                     (id) kCGImageSourceCreateThumbnailWithTransform : @YES,
                                                     (id) kCGImageSourceCreateThumbnailFromImageAlways : @YES,
                                                     (id) kCGImageSourceThumbnailMaxPixelSize : @(width)
                                                     };

CGImageRef scaledImageRef = CGImageSourceCreateThumbnailAtIndex(src, 0, options);
CFRelease(src);
UIImage *scaled = [UIImage imageWithCGImage:scaledImageRef];
CGImageRelease(scaledImageRef);
return scaled;

Note: If you have URL of image then you can create CGImageSourceRef using CGImageSourceCreateWithURL.

CGImageSourceRef src = CGImageSourceCreateWithURL((__bridge CFURLRef)(imageFileURL), NULL);
4
  • 1
    Missing CFRelease(src)?
    – Pang
    Mar 28, 2018 at 8:57
  • 1
    @Pang If you are facing memory leak than try once releasing src object.
    – Nirav D
    Mar 29, 2018 at 5:54
  • One of the least memory intensive options I've come across. Very useful, thank you.
    – dezinezync
    Dec 19, 2018 at 7:11
  • After doing CFRelease(src) still facing memory leak Sep 26, 2019 at 5:20
0

Swift and iOS 15

func preparingThumbnail(of size: CGSize) -> UIImage?

Example would be:

let image = UIImage(named: "info")
image.preparingThumbnail(of size: CFSize(width: 300, height: 400))
1
  • how to preserve aspect ratio? current it is not preserving Apr 20 at 7:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.