Skip to content

Instantly share code, notes, and snippets.

@juliomarcos
Created December 4, 2014 11:54
Show Gist options
  • Save juliomarcos/9abd2d53a19e37eaf20c to your computer and use it in GitHub Desktop.
Save juliomarcos/9abd2d53a19e37eaf20c to your computer and use it in GitHub Desktop.
Create iOS thumb UIImage
- (void)createThumbImageWithImage:(UIImage*)image
{
float radius = 19.0;
CGSize originalImageSize = image.size;
CGRect newRect = CGRectMake(0, 0, radius*2, radius*2);
float ratio = MAX(newRect.size.width / originalImageSize.width, newRect.size.height / originalImageSize.height);
UIGraphicsBeginImageContextWithOptions(newRect.size, NO, 0.0);
// circular mask
UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:CGPointMake(radius, radius)
radius:radius
startAngle:0
endAngle:360
clockwise:YES];
[path addClip];
// center crop
CGRect projectionRect = CGRectMake((newRect.size.width - ratio * originalImageSize.width) / 2,
(newRect.size.height - ratio * originalImageSize.height) / 2,
ratio * originalImageSize.width,
ratio * originalImageSize.height);
[image drawInRect:projectionRect];
self.thumbImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment