I'm trying to parse an image url through JSON in my iPhone application. My json model is built like this:
{
"picture":"link_to_image.jpg",
"about":"about text here",
"name":"Name"
}
I use this code to parse the itemw in my app:
- (void)fetchedData:(NSData *)responseData
{
NSError *error;
NSDictionary *json = [NSJSONSerialization JSONObjectWithData:responseData
options:kNilOptions error:&error];
self.titleLabel.text = [json objectForKey:@"name"];
self.aboutText.text = [json objectForKey:@"about"];
self.profileImage.image = [json objectForKey:@"picture"];
}
And in ViewDidLoad I wrote this:
dispatch_queue_t queue = dispatch_get_global_queue
(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_async(queue, ^{
NSData *data = [NSData dataWithContentsOfURL:
[NSURL URLWithString:@"link_to_my_json_file.php"]];
[self performSelectorOnMainThread:@selector(fetchedData:)
withObject:data waitUntilDone:YES];
});
I've connected the outlets to the items in my .xib-file and the title and about text are successfully parsed to the label and textview. But the image won't parse. The app keeps crashing when I try this for the image.
Can someone please explain what I'm doing wrong?
Thanks!