我正在构建一个 RSS 阅读器应用程序,遵循教程等等。
到目前为止,我已经构建了一个名为 blogPost 的自定义类,它存储了帖子名称和帖子作者,并具有基于名称的指定初始化程序。
我试图在我的 for 循环中提取帖子的缩略图,并将其显示在我当前显示标题和作者属性的单元格中。
我成功地提取了图像 URL 并从 JSON 中解析了它,但似乎无法将图像存储在 UIImage 中。
//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];
}