简体   繁体   中英

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?

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 .

You can't make changes to server from your ios device . you can just put data to server or fetch data from it. so, if big image is stored at server then you can't make it thumbnail from ios. you can just download it, once image comes to your device then you can modify it according to your need.

So, it is better way to keep two copies of same image to server and it is not hard task from server side. :)

When you have absolute URL and just want to display image using SDWebImage.

Use below code

SDWebImageManager *manager = [SDWebImageManager sharedManager];
    [manager downloadImageWithURL:[NSURL URLWithString:strDisplayURL]
                          options:0
                         progress:^(NSInteger receivedSize, NSInteger expectedSize) {
                             // progression tracking code
                         }
                        completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished, NSURL *imageURL) {

                            if (image)
                            {

                              dispatch_async(dispatch_get_main_queue(), ^{

                                    profileImageView.image = image;

                                });

                            }

                        }];

Use image modes.

在此处输入图片说明

And make corner radius half of image view height or width. so it will become circle image view.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM