0

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!

9
  • Maybe you could explain what you're doing first. Commented Oct 2, 2013 at 19:08
  • (You realize that you're putting an NSString pointer into self.profileImage.image, right? It's not an image, just its name.) Commented Oct 2, 2013 at 19:09
  • (And you also realize that, since you're dispatching that code async, the results from it will not be immediately available, right?) Commented Oct 2, 2013 at 19:10
  • And what messages do you get when the app crashes? Commented Oct 2, 2013 at 19:14
  • I'm sorry. I'm just getting started with Objective-C now... NSLog says: "Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFString _isResizable]:..." @HotLicks Commented Oct 2, 2013 at 19:17

1 Answer 1

1

As @Hot Licks have mentioned in comments you are putting a NSString pointer into UIImage property. Following method should work.

- (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"];
    NSURL *URL = [NSURL URLWithString: [json objectForKey:@"picture"]];
    dispatch_queue_t queue = dispatch_get_global_queue
    (DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
       dispatch_async(queue,  ^{
        NSData *data = [NSData dataWithContentsOfURL: URL];
        self.profileImage.image = [UIImage imageWithData: data];
    });
}
Sign up to request clarification or add additional context in comments.

4 Comments

But it needs to be noted that none of those values will be set on exit from the sequence in viewDidLoad, but at some later time.
I get "Parse Issue Expected identifier" on the line:NSURL *URL = [[NSURL URLWithString: [json objectForKey:@"picture"]];
@DavidGabor - Count your brackets.
I have removed one bracket.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.