0

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.

Simon
  • 451
  • 2
  • 9
  • 19
  • You should probably look into doing asynchronous calls and have a look at implementing the AFNetworking delegate if you aren't already. – danielbeard Apr 24 '12 at 02:07
  • Well, if this code is running on the main thread then the application will wait while it downloads each image (making the UI stop responding to input) using callbacks means that the app can download the images in the background while a user can continue to use the app. – danielbeard Apr 24 '12 at 02:17
  • thats what I need, for the user to continue to use the app, but as soon as the image get downloaded I need to call to reload data. Can I be notified when it will finish? I saw setImageWithURL:placeholderImage:success:failure: but I'm fairly new to iOS and dont know what to do with blocks. Any code will be welcomed – Simon Apr 24 '12 at 02:22
  • Sorry, my mistake AFNetworking adds a category to UIImageView that means that setImageWithURL is already loading async in the background. In that case, the image would not be set immediately after stepping through the code and would take a little while before it was downloaded. – danielbeard Apr 24 '12 at 02:22

1 Answers1

4

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
                   }
danielbeard
  • 9,120
  • 3
  • 44
  • 58
  • Thats what i tried but i got this error Semantic Issue: Incompatible block pointer types sending 'void (^)(void)' to parameter of type 'void (^)(NSURLRequest *__strong, NSHTTPURLResponse *__strong, UIImage *__strong)' I guess i should be some how passing this to the block? – Simon Apr 24 '12 at 02:30
  • got it working, thanks for taking the time to help me. Cheers – Simon Apr 24 '12 at 02:44
  • [image setImageWithURLRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://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 } – Asad Khan Nov 30 '12 at 11:21
  • 1
    code in the answer will crash at runtime this method takes NSURLRequest as parameter – Asad Khan Nov 30 '12 at 11:22