将JSON中的图像存储到自定义类iOS中
我正在构建一个RSS阅读器应用程序,遵循教程以及其他内容 到目前为止,我已经构建了一个名为blogPost的自定义类,它存储了帖子名称和帖子作者,并基于该名称指定了一个初始值设定项 我试图在for循环中提取文章的缩略图,并将其显示在当前显示标题和作者属性的单元格中 我成功地提取了图像URL并从JSON解析了它,但似乎无法将图像存储在UIImage中将JSON中的图像存储到自定义类iOS中,ios,json,uiimage,Ios,Json,Uiimage,我正在构建一个RSS阅读器应用程序,遵循教程以及其他内容 到目前为止,我已经构建了一个名为blogPost的自定义类,它存储了帖子名称和帖子作者,并基于该名称指定了一个初始值设定项 我试图在for循环中提取文章的缩略图,并将其显示在当前显示标题和作者属性的单元格中 我成功地提取了图像URL并从JSON解析了它,但似乎无法将图像存储在UIImage中 //Custom header for BlogPost @interface BlogPost : NSObject @property (no
//Custom header for BlogPost
@interface BlogPost : NSObject
@property (nonatomic, strong) NSString *title;
@property (nonatomic, strong) NSString *author;
@property (nonatomic, strong) UIImage *image;
// Designated Initializer
- (id) initWithTitle:(NSString *)title;
+ (id) blogPostWithTitle:(NSString *)tile;
@end
这是tableViewController
[super viewDidLoad];
NSURL *blogUrl = [NSURL URLWithString:@"http://www.wheninmanila.com/api/get_recent_summary/"];
NSData *jsonData = [NSData dataWithContentsOfURL:blogUrl];
NSError *error = nil;
NSDictionary *dataDictionary = [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:&error];
self.blogPosts = [NSMutableArray array];
NSArray *blogPostsArray = [dataDictionary objectForKey:@"posts"];
for (NSDictionary *bpDictionary in blogPostsArray) {
BlogPost *blogPost = [BlogPost blogPostWithTitle:[bpDictionary objectForKey:@"title"]];
blogPost.author = [bpDictionary objectForKey:@"author"];
NSURL *thumbURL = [bpDictionary objectForKey:@"thumbnail"];
NSData *thumbData = [NSData dataWithContentsOfURL:thumbURL];
blogPost.image = [[UIImage alloc] initWithData:thumbData];
[self.blogPosts addObject:blogPost];
}
更改此行:
NSURL *thumbURL = [bpDictionary objectForKey:@"thumbnail"];
为此:
NSURL *thumbURL = [NSURL urlWithString:[bpDictionary objectForKey:@"thumbnail"]];
词典中的值将是
NSString
,与NSURL
不同。您使用的是NSURL
而不是NSString
,NSString
不响应选择器isFileURL
(这就是您得到异常的原因)。我假设您的缩略图是一个字符串,因此您应该将其作为NSString
获取,然后将其转换为NSURL
,如下所示:
NSString *thumbAsString = [bpDictionary objectForKey:@"thumbnail"];
NSURL *thumbURL = [NSURL URLWithString:thumbAsString];
当我尝试运行时,应用程序崩溃。排除错误[\uu NSCFString isFileURL]:发送到实例的无法识别的选择器