iOS Create Thumbnail by means of offscreen image context

- (void)setThumbnailFromImage:(UIImage *)image {
    CGSize origImageSize = image.size;
    CGRect newRect = CGRectMake(0, 0, 40, 40);
    float ratio = MAX(newRect.size.width / origImageSize.width, newRect.size.height / origImageSize.height);

    UIGraphicsBeginImageContextWithOptions(newRect.size, NO, ratio);

    UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:newRect cornerRadius:5.0];
    [path addClip];
    CGRect thumbRect;
    thumbRect.size.width = ratio * origImageSize.width;
    thumbRect.size.height = ratio * origImageSize.height;
    thumbRect.origin.x = (newRect.size.width - thumbRect.size.width) / 2.0;
    thumbRect.origin.y = (newRect.size.height - thumbRect.size.height) / 2.0;
    [image drawInRect:thumbRect];
    UIImage *thumbnail = UIGraphicsGetImageFromCurrentImageContext();

    //Get the thumbnail
    //self.thumbnail = thumbnail;
    
    UIGraphicsEndImageContext();
}