I’m trying to write a convenience function that will accept an image identifier and download an image using AFNetworking’s AFImageRequestOperation. The function downloads the image properly, but I can’t return the UIImage in the success block.
-(UIImage *)downloadImage:(NSString*)imageIdentifier
{
NSString* urlString = [NSString stringWithFormat:@"http://myserver.com/images/%@", imageIdentifier];
AFImageRequestOperation* operation = [AFImageRequestOperation imageRequestOperationWithRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:urlString]] imageProcessingBlock:nil
success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image)
{
NSLog(@"response: %@", response);
return image;
}
failure:nil];
[operation start];
}
The return image; line gives me the error:
Incompatible block pointer types sending 'UIImage *(^)(NSURLRequest *__strong, NSHTTPURLResponse *__strong, UIImage *__strong)' to parameter of type 'void (^)(NSURLRequest *__strong, NSHTTPURLResponse *__strong, UIImage *__strong)'
Any ideas of what’s going on? I’d love to be able to just call
UIImage* photo = [downloadImage:id_12345];
AFNetworking image download operation is asynchronous, you can’t assign it at the point of operation start.
The function you’re trying to build should either use delegates or blocks.
call it like this