Question

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;

}
Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top