我正在尝试使用 SDWebImage 在 UIImageView 中显示来自 JSON url 的图像,类似于 Tinder。我遇到的每个教程都只处理下载单个图像 url (@"suchandsuch.png") 或在 tableview 中显示图像(我不想这样做)。
不确定我做错了什么。
我的示例代码:ViewController.m
-(void)viewDidLoad{
[super viewDidLoad];
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
[manager GET:@"http://www.suchandsuch.com/api/sites" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
self.posts = (NSDictionary *) responseObject;
self.post = self.posts[@"sites"];
NSLog(@"JSON: %@", self.post);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error: %@", error);
}];
NSData *data = [[NSData alloc] initWithContentsOfURL:
[NSURL URLWithString:@"http://www.suchandsuch.com/api/sites"]];
NSError *error;
NSDictionary *jsonDict = [NSJSONSerialization
JSONObjectWithData:data
options:kNilOptions
error:&error];
NSString *logoString = [jsonDict objectForKey:@"logoURL"];
NSURL *logoURL = [NSURL URLWithString:logoString];
[self.myImage setImageWithURL:logoURL];
}
最佳答案
我认为您已经将 SDWebImage 文件添加到您的项目中,并且您知道如何解析 JSON,这就是您需要做的所有事情。将此行添加到 View Controller 类的顶部 -
#import "UIImageView+Webcache.h
在您解析和检索对象后,包含您需要的 imageUrl,例如从 JSON 中提取的字典 (dict)
例子
NSString *jsonImageUrlString = [dict objectForKey:@"imageUrl"];
NSURL *imageURL = [NSURL URLWithString:jsonImageUrlString];
[[SDImageCache sharedImageCache] removeImageForKey:imageURL fromDisk:YES];
[self.profileImageView setImageWithURL:imageURL];
希望对你有帮助
编辑 您的项目中应该已经有的 SDWebImage 文件
关于ios - 如何使用 SDWebImage 将图像从 JSON 加载到 UIImageView?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24984949/