Search code examples
iosobjective-csdwebimage

How to get thumbnail image using SDWebImage?


I do not want to download the image from the server until the user selects it. I want to show the thumbnails for the image. How to get thumbnails? I am able to download the full image using the URL from the server.

If the image at server is of say 1MB, how to get its thumbnail version?


Solution

  • There are two ways to achieve your goal :

    Way 1:

    You can not get thumbnail. If you have file of size say 1 MB then you have to downloaded it using SDWebImage. When download completes create thumbnail of image. Now you can remove image of 1MB.

    Code for create thumbnail :

    + (UIImage *)imageWithImage:(UIImage *)image scaledToFillSize:(CGSize)size
    {
    
    CGFloat scale = MAX(size.width/image.size.width, size.height/image.size.height);
    CGFloat width = image.size.width * scale;
    CGFloat height = image.size.height * scale;
    CGRect imageRect = CGRectMake((size.width - width)/2.0f,
                                  (size.height - height)/2.0f,
                                  width,
                                  height);
    
    UIGraphicsBeginImageContextWithOptions(size, NO, 0);
    [image drawInRect:imageRect];
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();    
    UIGraphicsEndImageContext();
    return newImage;
    }
    

    Way 2:

    At server side you have to keep both image original image and thumbnails. And so first load thumbnail using SDWebImage. When user click on thumbnail get original image from server using SDWebImage.