title: Square thumbnail from UIImagePickerViewController image
body: <p>In my app the user selects an image or take a picture using UIImagePickerViewController. Once the image was selected I want to display its thumbnail on a square UIImageView (90x90).</p>
  
  <p>I'm using <a href="http://developer.apple.com/library/mac/#documentation/GraphicsImaging/Conceptual/ImageIOGuide/imageio_source/ikpg_source.html#//apple_ref/doc/uid/TP40005462-CH218-DontLinkElementID_5" rel="nofollow noreferrer">Apple's code</a> to create a thumbnail. The problem is the thumbnail is not squared, the function, after set kCGImageSourceThumbnailMaxPixelSize key to 90, seems only to resize the image's height, and as far as I know the kCGImageSourceThumbnailMaxPixelSize key should be responsible for setting the height and width of the thumbnail.</p>
  
  <p>Here is a glimpse of my code:</p>
  
  <pre><code>- (void)imagePickerController:(UIImagePickerController *)picker
      didFinishPickingMediaWithInfo:(NSDictionary *)info {
  
      UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
  
      NSData *imageData = UIImageJPEGRepresentation (image, 0.5);
  
      // My image view is 90x90
      UIImage *thumbImage = MyCreateThumbnailImageFromData(imageData, 90);
  
      [myImageView setImage:thumbImage];
  
      if (picker.sourceType == UIImagePickerControllerSourceTypeCamera) {
  
          UIImageWriteToSavedPhotosAlbum(image, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
      }
  
      [picker dismissViewControllerAnimated:YES completion:nil];
  }
  
  UIImage* MyCreateThumbnailImageFromData (NSData * data, int imageSize) {
  
      CGImageRef        myThumbnailImage = NULL;
      CGImageSourceRef  myImageSource;
      CFDictionaryRef   myOptions = NULL;
      CFStringRef       myKeys[3];
      CFTypeRef         myValues[3];
      CFNumberRef       thumbnailSize;
  
      // Create an image source from NSData; no options.
      myImageSource = CGImageSourceCreateWithData((__bridge CFDataRef)data,
                                                  NULL);
  
      // Make sure the image source exists before continuing.
      if (myImageSource == NULL){
          fprintf(stderr, "Image source is NULL.");
          return  NULL;
      }
  
      // Package the integer as a  CFNumber object. Using CFTypes allows you
      // to more easily create the options dictionary later.
      thumbnailSize = CFNumberCreate(NULL, kCFNumberIntType, &amp;imageSize);
  
      // Set up the thumbnail options.
      myKeys[0] = kCGImageSourceCreateThumbnailWithTransform;
      myValues[0] = (CFTypeRef)kCFBooleanTrue;
      myKeys[1] = kCGImageSourceCreateThumbnailFromImageIfAbsent;
      myValues[1] = (CFTypeRef)kCFBooleanTrue;
      myKeys[2] = kCGImageSourceThumbnailMaxPixelSize;
      myValues[2] = thumbnailSize;
  
      myOptions = CFDictionaryCreate(NULL, (const void **) myKeys,
                                     (const void **) myValues, 2,
                                     &amp;kCFTypeDictionaryKeyCallBacks,
                                     &amp; kCFTypeDictionaryValueCallBacks);
  
      // Create the thumbnail image using the specified options.
      myThumbnailImage = CGImageSourceCreateThumbnailAtIndex(myImageSource,
                                                             0,
                                                             myOptions);
  
      UIImage* scaled = [UIImage imageWithCGImage:myThumbnailImage];
  
      // Release the options dictionary and the image source
      // when you no longer need them.
  
      CFRelease(thumbnailSize);
      CFRelease(myOptions);
      CFRelease(myImageSource);
  
      // Make sure the thumbnail image exists before continuing.
      if (myThumbnailImage == NULL) {
          fprintf(stderr, "Thumbnail image not created from image source.");
          return NULL;
      }
      return scaled;
  }
  </code></pre>
  
  <p>And this how my image view is instantiated:</p>
  
  <pre><code>myImageView = [[UIImageView alloc] init];
  imageView.contentMode = UIViewContentModeScaleAspectFit;
  
  CGRect rect = imageView.frame;
  rect.size.height = 90;
  rect.size.width = 90;
  
  imageView.frame = rect;
  [imageView setUserInteractionEnabled:YES];
  </code></pre>
  
  <p>If I don't set <code>imageView.contentMode = UIViewContentModeScaleAspectFit;</code> the thumbnail will be distorted, since it is just a version of my original image with height of 90 pixels.</p>
  
  <p>So, why is my thumbnail not squared?</p>