简体   繁体   English

如何使用SDWebImage获取缩略图?

[英]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. 我可以使用服务器上的URL下载完整图像。

If the image at server is of say 1MB, how to get its thumbnail version? 如果服务器上的图像约为1MB,如何获取其缩略图版本?

There are two ways to achieve your goal : 有两种方法可以实现您的目标:

Way 1: 方法1:

You can not get thumbnail. 您无法获取缩略图。 If you have file of size say 1 MB then you have to downloaded it using SDWebImage . 如果文件大小为1 MB,则必须使用SDWebImage下载。 When download completes create thumbnail of image. 下载完成后,创建图像缩略图。 Now you can remove image of 1MB. 现在您可以删除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: 方式2:

At server side you have to keep both image original image and thumbnails. 在服务器端,您必须保留图像原始图像和缩略图。 And so first load thumbnail using SDWebImage . 因此,首先使用SDWebImage加载缩略图。 When user click on thumbnail get original image from server using SDWebImage . 当用户单击缩略图时,使用SDWebImage从服务器获取原始图像。

You can't make changes to server from your ios device . 您无法通过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. 因此,如果大图像存储在服务器上,则无法从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. 当您具有绝对URL且仅想使用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. 因此它将变成圆形图像视图。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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