我无法使用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) {
}];
尝试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;
} }];
} }];
尝试3:
[[SDWebImageDownloader sharedDownloader] downloadImageWithURL:[NSURL URLWithString:thumbnailURLString] options:SDWebImageDownloaderUseNSURLCache progress:nil completed:^(UIImage *image, NSData *data, NSError *error, BOOL finished) {
}];
尝试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) {
}];
尝试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;
}];
在上述所有情况下,都会出现以下错误:
Error Domain = NSURLErrorDomain代码= -999“已取消” 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 = cancelled}
之前,我们使用的是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];
}
}];
请告诉我是否有任何错误或建议。
更新1: 尝试使用邮递员下载图像。请检查图像。 升级到SDWebImage v4.4.5后,相同的图像URL可以在SDWebImage v3.5.4上运行,它不能容纳该图像。
我是否缺少任何配置?还安装了pod'SDWebImage / WebP',并在Build settings-> Preprocessor macros中添加了SD_WEBP = 1。
错误:错误域= SDWebImageErrorDomain代码= 0“下载的图像的像素为0” UserInfo = {NSLocalizedDescription =下载的图像的像素为0}
http://testingurl.net/api/Image?ActionFlagId=20&id=864
授权: 电子邮件:kkk1@ler.com, 令牌:A-BE3D-WER3-847F-ER6YG5678G
答案 0 :(得分:4)
基本上,如果URL中包含图像,SDWebImage可以从任何URL下载它们。
但是,在您的情况下,您尝试从JSON获取图像。您的服务器API返回一个JSON对象,其中包含图像为base64String
。当您获得JSON响应时,您需要从json获取base64String
,然后从base64String
创建图像。
您可以使用此方法使用Objective C
将字符串解码为图像。
- (UIImage *)decodeBase64ToImage:(NSString *)strEncodeData {
NSData *data = [[NSData alloc]initWithBase64EncodedString:strEncodeData options:NSDataBase64DecodingIgnoreUnknownCharacters];
return [UIImage imageWithData:data];
}
答案 1 :(得分:1)
首先,更新pod并尝试使用SDWebImage pod 3.8.2版。
第二件事是检查您的图像URL。因为我亲自使用此pod版本测试了图片下载,并且下载功能正常工作。
答案 2 :(得分:0)
答案 3 :(得分:0)
我遇到了同样的问题,并且尝试使用webP图像。后来我意识到我需要将webP编码器添加到SDImageCodersManager。
SDImageCodersManager.shared.addCoder(SDImageWebPCoder.shared)
我在AppDelegate中做到了。