使用 SDWebImage 下载图像,错误 -999(或)SDWebImageErrorDomain Code=0 “下载的图像有 0 像素

Bab*_*bul 5 iphone objective-c ios sdwebimage

我无法使用 SDWebImage 4.4.5 版本在 Objective c 中下载图像。我尝试了以下方法,但没有任何效果。

尝试 1:

[self.thumbnailImageView sd_setImageWithURL:[NSURL URLWithString:thumbnailURLString] completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
Run Code Online (Sandbox Code Playgroud)

}];

尝试2:

[self.thumbnailImageView sd_setImageWithURL:[NSURL URLWithString:thumbnailURLString] placeholderImage:nil options:SDWebImageRefreshCached completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) { if(image != nil){
            self.thumbnailImageView.image = image;                
        } else {
            NSLog(@"Photo Not Available, fetch");
            [self.thumbnailImageView sd_setImageWithURL:[NSURL URLWithString:thumbnailURLString] placeholderImage:nil options:SDWebImageRefreshCached completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
                if(image != nil) { NSLog(@"Lo Res Image Loaded by %ld from '%@'", (long)cacheType, imageURL);
                    self.thumbnailImageView.image = image;

                } else {
                    self.thumbnailImageView.image = nil;

                } }];

        } }];
Run Code Online (Sandbox Code Playgroud)

尝试3:

[[SDWebImageDownloader sharedDownloader] downloadImageWithURL:[NSURL URLWithString:thumbnailURLString] options:SDWebImageDownloaderUseNSURLCache progress:nil completed:^(UIImage *image, NSData *data, NSError *error, BOOL finished) {


        }];
Run Code Online (Sandbox Code Playgroud)

尝试4:

SDWebImageManager *manager = [SDWebImageManager sharedManager];

        [manager loadImageWithURL:[NSURL URLWithString:thumbnailURLString] options:SDWebImageDelayPlaceholder progress:^(NSInteger receivedSize, NSInteger expectedSize, NSURL * _Nullable targetURL) {
        } completed:^(UIImage * _Nullable image, NSData * _Nullable data, NSError * _Nullable error, SDImageCacheType cacheType, BOOL finished, NSURL * _Nullable imageURL) {

        }];
Run Code Online (Sandbox Code Playgroud)

尝试5:

SDWebImageDownloader *manager = [SDWebImageManager sharedManager].imageDownloader;
        [manager setValue:@"application/vnd.learning.events.v1+json" forHTTPHeaderField:@"Accept"];
        [manager setValue:@"en-US" forHTTPHeaderField:@"Accept-Language"];
        [manager setValue:@"application/json; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
        //[manager setValue:savedEmail forHTTPHeaderField:@"Email"];
        [manager setValue:savedToken forHTTPHeaderField:@"Authorization"];
        [manager downloadImageWithURL:[NSURL URLWithString:thumbnailURLString] options:SDWebImageDownloaderHighPriority progress:^(NSInteger receivedSize, NSInteger expectedSize, NSURL * _Nullable targetURL) {
        } completed:^(UIImage * _Nullable image, NSData * _Nullable data, NSError * _Nullable error, BOOL finished) {
            self.thumbnailImageView.image = image;
        }];
Run Code Online (Sandbox Code Playgroud)

在上述所有情况下,都会出现以下错误:

错误域=NSURLErrorDomain Code=-999 "cancelled" UserInfo={NSErrorFailingURLStringKey= https://thisisnotcorrecturl.net/api/BAImage?ActionFlagId=97&id=615 , NSErrorFailingURLKey= https://thisisnotcorrecturl.net/api/BAImage?ActionFlagId= 97&ID = 615,_NSURLErrorRelatedURLSessionTaskErrorKey =( “LocalDataTask <92586538-B07D-46FB-8694-3B29AA3F0CB4> <7>”),_NSURLErrorFailingURLSessionTaskErrorKey = LocalDataTask <92586538-B07D-46FB-8694-3B29AA3F0CB4> <7>,NSLocalizedDescription =取消}

之前我们用的是v3.5.4版本,可以用下面的代码下载镜像。它运作良好。

[self.thumbnailImageView setImageWithURL:[NSURL URLWithString:thumbnailURLString]
                            placeholderImage:nil
                                   completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType) {
                                       if (image != nil){
                                           self.thumbnailImageView.image = [self imageResizingToAspetRatio:image];

                                       }
                                   }];
Run Code Online (Sandbox Code Playgroud)

如果有任何错误或任何建议,请告诉我。

更新 1:尝试使用邮递员下载图像。请检查图像。在我升级到 SDWebImage v4.4.5 后,相同的图像 URL 正在 SDWebImage v3.5.4 上工作,它没有放置图像。

我缺少任何配置吗?还安装了 pod 'SDWebImage/WebP',在 Build settings -> Preprocessor macros 中添加了 SD_WEBP=1。

错误:Error Domain=SDWebImageErrorDomain Code=0 “下载的图像有 0 个像素” UserInfo={NSLocalizedDescription=下载的图像有 0 个像素}

在此处输入图片说明

http://testingurl.net/api/Image?ActionFlagId=20&id=864

授权:邮箱:kkk1@ler.com,代币:A-BE3D-WER3-847F-ER6YG5678G

Fay*_*med 4

基本上,如果 URL 包含图像,SDWebImage 从任何 URL 下载它们都没有问题。

但就您而言,您尝试从 JSON 获取图像。您的服务器 API 返回一个 JSON 对象,其中包含base64String. 当您获得 JSON 响应时,您需要base64String从 json 获取,然后从base64String.

您可以使用此方法将字符串解码为图像Objective C

- (UIImage *)decodeBase64ToImage:(NSString *)strEncodeData {
  NSData *data = [[NSData alloc]initWithBase64EncodedString:strEncodeData options:NSDataBase64DecodingIgnoreUnknownCharacters];
  return [UIImage imageWithData:data];
}
Run Code Online (Sandbox Code Playgroud)