3

This code is from Apple's WWDC 2011 Session 318 - iOS Performance in Depth and uses CoreGraphics to create thumbnails from server hosted images.

CGImageSourceRef src = CGImageSourceCreateWithURL(url);
NSDictionary *options = (CFDictionaryRef)[NSDictionary 
dictionaryWithObject:[NSNumber numberWithInt:1024
forKey:(id)kCGImageSourceThumbnailMaxPixelSize];

CGImageRef thumbnail = CGImageSourceCreateThumbnailAtIndex(src,0,options);
UIImage *image = [UIImage imageWithCGImage:thumbnail];
CGImageRelease(thumbnail);
CGImageSourceRelease(src); 

But it doesnt work and the docs don't really help. In the iOS docs CGImageSource CGImageSourceRef CGImageSourceCreateThumbnailAtIndex are available

in Mac OS X v10.4 or later

How can I get this to work?

EDIT

These are the compiler errors I'm getting:

  • Use of undeclared identifier 'CGImageSourceRef'
  • Use of undeclared identifier 'kCGImageSourceThumbnailMaxPixelSize'
  • Use of undeclared identifier 'src'
  • Implicit declaration of function 'CGImageSourceCreateThumbnailAtIndex' is invalid in C99
  • Implicit declaration of function 'CGImageSourceRelease' is invalid in C99
  • Implicit declaration of function 'CGImageSourceCreateWithURL' is invalid in C99
daihovey
  • 3,485
  • 13
  • 66
  • 110
  • 1
    iOS docs are actually [here](http://developer.apple.com/library/ios/#documentation/GraphicsImaging/Reference/CGImageSource/Reference/reference.html) and availabilaty is declared `in iOS 4.0 and later`. Try to state the exact source of problem. Do you even get a valid `src`. P.S. it's not a good idea to discuss WWDC sessions outside apple.developer forum - since all of referenced functions are not under NDA i guess you're o.k. :) – Rok Jarc Jun 12 '12 at 09:41
  • Ah yep didnt see that iOS 4+ further down. Edited with errors. Also, this is from last years WWDC, which is fine no? – daihovey Jun 12 '12 at 09:58
  • I guess it should be fine though i remember seing disclamer somewhere. – Rok Jarc Jun 12 '12 at 10:11

2 Answers2

15

School boy mistake.

Didn't add #import <ImageIO/ImageIO.h>

daihovey
  • 3,485
  • 13
  • 66
  • 110
3

Try image resize:

-(UIImage*) resizedImage:(UIImage *)inImage:(CGRect) thumbRect
{
    CGImageRef          imageRef = [inImage CGImage];
    CGImageAlphaInfo    alphaInfo = CGImageGetAlphaInfo(imageRef);

    // There's a wierdness with kCGImageAlphaNone and CGBitmapContextCreate
    // see Supported Pixel Formats in the Quartz 2D Programming Guide
    // Creating a Bitmap Graphics Context section
    // only RGB 8 bit images with alpha of kCGImageAlphaNoneSkipFirst, kCGImageAlphaNoneSkipLast, kCGImageAlphaPremultipliedFirst,
    // and kCGImageAlphaPremultipliedLast, with a few other oddball image kinds are supported
    // The images on input here are likely to be png or jpeg files
    if (alphaInfo == kCGImageAlphaNone)
        alphaInfo = kCGImageAlphaNoneSkipLast;

    // Build a bitmap context that's the size of the thumbRect
    CGContextRef bitmap = CGBitmapContextCreate(
                                                NULL,
                                                thumbRect.size.width,       // width
                                                thumbRect.size.height,      // height
                                                CGImageGetBitsPerComponent(imageRef),   // really needs to always be 8
                                                4 * thumbRect.size.width,   // rowbytes
                                                CGImageGetColorSpace(imageRef),
                                                alphaInfo
                                                );

    // Draw into the context, this scales the image
    CGContextDrawImage(bitmap, thumbRect, imageRef);

    // Get an image from the context and a UIImage
    CGImageRef  ref = CGBitmapContextCreateImage(bitmap);
    UIImage*    result = [UIImage imageWithCGImage:ref];

    CGContextRelease(bitmap);   // ok if NULL
    CGImageRelease(ref);

    return result;
}

i use it in my code for awhile but i can't remember it's source

try this also resizing a UIImage without loading it entirely into memory?

Community
  • 1
  • 1
Mina Nabil
  • 676
  • 6
  • 19
  • Thanks for this, but I specifically wanted to use the `CGImageSourceCreateThumbnailAtIndex` function – daihovey Jun 12 '12 at 10:01
  • 1
    http://stackoverflow.com/questions/5860215/resizing-a-uiimage-without-loading-it-entirely-into-memory try this please – Mina Nabil Jun 12 '12 at 10:03