Search code examples
objective-ciosuiimageviewafnetworkingicarousel

Get UIImageView using Afnetworking and put it in an array


As the title says, i need to get the image and put it in an array.

UIImageView *myImage = [[UIImageView alloc] init];
[myImage setImageWithURL:[NSURL URLWithString:@"http://host.com/images/1/1.png"] placeholderImage:[UIImage imageNamed:@"page3.png"]];
[items addObject:myImage]; 

the problem is that while the image is being loaded, the code continues and nothing gets put in the array, actually I guess an empty UIImageView gets inserted.

How do I go about fixing this?

On a side note I am using iCarousel to display images, in the data source it gets the images array and shows them. Can i modify iCarousel somehow?

Thanks in advance.


Solution

  • You can use the setImageWithURLRequest method to give a success message or execute the UI update when it has finished for example using the following method:

    - (void)setImageWithURLRequest:(NSURLRequest *)urlRequest 
              placeholderImage:(UIImage *)placeholderImage 
                       success:(void (^)(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image))success
                       failure:(void (^)(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error))failure;
    

    Here is an example (not tested)

    [image setImageWithURLRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"host.com/images/1/1.png"]] placeholderImage:[UIImage imageNamed:@"page3.png"] 
                       success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image){ 
                                 //reload data here
                       }
                       failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error){
                                 //handle errors here
                       }