0

I'm trying to display an image returned from AFImageRequestOperation ImageRequestOperationWithRequest: but it appears that the image is only available inside the success block. I've tried saving it to a class instance variable but when I NSLog it, it shows up as null. However, when I NSLog the same instance variable inside the success block, after setting it with the retrieved UIImage, it actually shows a hexadecimal value (as expected). Any help here is greatly appreciated.

Here's the code where issue occurs:

imageRequest = [AFImageRequestOperation imageRequestOperationWithRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:urlString]] success:^(UIImage *image) {
    globalImage = image; // trying to have accessible image outside of success handler
    [self setImageThumbImage:image]; trying to set image that class will use 
    NSLog(@"Image is: %@", self.albumThumbImage); // logs an actual value
    imageRequest = nil;
}];

[albumImageRequest start];
NSLog(@"The second albumThumbImage retrieval returns: %@", globalImage); // logs null

return self;

}
davetw12
  • 1,815
  • 2
  • 20
  • 27

1 Answers1

0

Why not just use the added methods to UIImageView provided by AFNetworking?

From the git page:

UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 100.0f, 100.0f)];
[imageView setImageWithURL:[NSURL URLWithString:@"http://i.imgur.com/r4uwx.jpg"] placeholderImage:[UIImage imageNamed:@"placeholder-avatar"]];

I know this doesn't answer your question directly though it seems to achieve what you are looking to do in a much simpler manner.

NSTJ
  • 3,858
  • 2
  • 27
  • 34
  • Yeah, that gets the job done but I still want to be able to save the image after it is displayed. Since setImageWithURL: does not return a value its not possible to do so with it. – davetw12 Oct 24 '12 at 04:14
  • I guess I'll just do it that way and save it the old-fashioned way with NSData. Thanks! – davetw12 Oct 24 '12 at 04:22
  • 2
    The reason the NSLog which you are showing above returns `nil` is because the image has not been downloaded when the NSLog has been run (remember your AFNetworking request is an asynchronous call). The problem you are having is likely in the way you are setting the image in your call to `setImageThumbImage:image`. Are you redrawing in this method? – NSTJ Oct 24 '12 at 07:58