【问题标题】:How to set progressive image in iOS using SDWebImage?如何使用 SDWebImage 在 iOS 中设置渐进式图像?
【发布时间】:2016-08-10 00:18:39
【问题描述】:

我尝试在我的应用中逐步加载图像。基本上我尝试的是在加载图像时,我想从模糊状态显示完全加载的图像。我试过了,

SDWebImageManager *manager = [SDWebImageManager sharedManager];
[manager downloadImageWithURL:[NSURL URLWithString:@"https://profile.microsoft.com/RegsysProfileCenter/Images/personal_info.jpg"]
                      options:0
                     progress:^(NSInteger receivedSize, NSInteger expectedSize) {
                         [self.profileBtn setImage:[UIImage imageWithData:[NSData dataWithBytes:&receivedSize length:sizeof(receivedSize)] scale:15] forState:UIControlStateNormal];
                     }
                    completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished, NSURL *imageURL) {
                        if (image) {
                            // [self.profileBtn setImage:image forState:UIControlStateNormal];
                        }
                    }];

是否可以使用 SDWebImage 逐步加载图像。请帮我解决这个问题。

【问题讨论】:

标签: ios objective-c sdwebimage


【解决方案1】:

你可以试试这个实现

+ (void)loadImageWithURLString:(NSString *)urlString forImageView:(UIImageView *)imageView {
    [imageView sd_setImageWithURL:[Utilities stringToURL:urlString]
                 placeholderImage:[UIImage placeHolderImage]
                          options:SDWebImageProgressiveDownload];
}

选项说

/**
     * This flag enables progressive download, the image is displayed progressively during download as a browser would do.
     * By default, the image is only displayed once completely downloaded.
     */
    SDWebImageProgressiveDownload = 1 << 3,

【讨论】:

  • 请注意,这需要使用 Progressive JPEG 压缩保存文件。
  • 如果您想从模糊状态显示图像,我认为一种可能的解决方案是在占位符图像顶部创建一个模糊层,模糊值由下载完成的百分比设置。跨度>
【解决方案2】:

您可以在临时标签或活动指示器下方显示下载百分比(可以在图像视图或按钮上显示活动指示器,直到下载未完成),例如进行中的块,

 NSInteger downloadCompleted = (receivedSize/ expectedSize) * 100;
 label.text = downloadCompleted; //temp label on image view or below activityindicator

希望这会有所帮助:)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-03-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-27
    • 2013-01-16
    相关资源
    最近更新 更多