Created
December 4, 2014 11:54
-
-
Save juliomarcos/9abd2d53a19e37eaf20c to your computer and use it in GitHub Desktop.
Create iOS thumb UIImage
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- (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