jac300
jac300

Reputation: 5222

Using NSURLConnection sendAsynchronousRequest

I have an application in Xcode 4.5.2. It sends a URL request to download an image and sets the image in an image view. I have the following code that works fine to accomplish this:

dispatch_queue_t downloadQueue = dispatch_queue_create("Get Facebook Friend", NULL);
    dispatch_async(downloadQueue, ^{

self.firstFriendImage = [UIImage imageWithData:
                           [NSData dataWithContentsOfURL:
                            [NSURL URLWithString:
                             [[self.facebookPhotosAll objectAtIndex:self.randomIndex1]
                              objectForKey:@"pic_big"]]]];

dispatch_async(dispatch_get_main_queue(), ^{
            [self postDownloadTasks:self.topView setLabel:self.firstFriendLabel 
withFriendName:self.firstFriendName cropImage:self.firstFriendImage 
inImageView:self.friendOneView atYPoint:22];
  });
      });

So although this code works fine, being new to objective C, I am trying to explore the language a bit to see how else I can do this same thing. So I tried to use the NSURLConnection sendAsynchronousRequest: method (after looking at some examples on here), but I can't seem to get this method to work. This is what I did:

 NSOperationQueue *queue = [[NSOperationQueue alloc]init];
NSString *string = [[self.facebookPhotosAll  
                   objectAtIndex:self.randomIndex1]objectForKey:@"pic_big"];

    [NSURLConnection sendAsynchronousRequest: [NSURL URLWithString:   
           string] queue:queue   
             completionHandler:^(NSURLResponse *response, NSData *data, NSError *error){
        // Make sure eveything is ok
        if(error){
            // do something
        }

        self.firstFriendImage = [UIImage imageWithData:data];
         dispatch_async(dispatch_get_main_queue(), ^{

            [self postDownloadTasks:self.topView setLabel:self.firstFriendLabel 
withFriendName:self.firstFriendName cropImage:self.firstFriendImage  
inImageView:self.friendOneView atYPoint:22];

         });

    }];

So this code doesn't work at all (it's not returning any data). The app crashes when the method is called and I get the following message:

** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSURL _CFURLRequest]: unrecognized selector sent to instance 0xa39dd20'

Can anyone tell me how to accomplish what I did in the first code excerpt using the NSURLConnection sendAsynchronousRequest method?

Upvotes: 2

Views: 1538

Answers (1)

rdelmar
rdelmar

Reputation: 104092

In the send asynchronousRequest method, your URL string is missing a part. It should be like in your first method that worked:

[[self.facebookPhotosAll objectAtIndex:self.randomIndex1] objectForKey:@"pic_big"]

Upvotes: 2

Related Questions