0

I download Images Using ASIHTTPRequest. I wrote This Code:

+(void)GetImagesURL
{
    NSLog(@"%i",[TripsArray count]);
    for (int i=0;i< [TripsArray count]; i++) 
    {

        NSURL *DetailTripURL = [NSURL URLWithString:[[TripsArray objectAtIndex:i] TripURL]];
        __block ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:DetailTripURL];
        [request setCompletionBlock:^{
            NSData *TripHTMLData = [request responseData];
            NSLog(@"url :  %@",DetailTripURL);
            [[TripsArray objectAtIndex:i] setTripData:TripHTMLData];
            //NSLog(@"%@",DetailTripHTMLData);
            // 2
            TFHpple *DetailTripParser = [TFHpple hppleWithHTMLData:TripHTMLData];

            // Get Image
            NSString *ImageTripXpathQueryString = @"//div[@class='image_container']/img";
            NSArray *ImageTripNodes = [DetailTripParser searchWithXPathQuery:ImageTripXpathQueryString];
            NSLog(@"%i",[ImageTripNodes count]);
            for (int j=0 ;j<[ImageTripNodes count]; j++)
            {
                [[TripsArray objectAtIndex:i] setImageUrl:[[ImageTripNodes objectAtIndex:j] objectForKey:@"src"]];
                NSLog(@"%@",[[ImageTripNodes objectAtIndex:j] objectForKey:@"src"]);
            }


        }];
        [request setFailedBlock:^{
            NSError *error = [request error];
            NSLog(@"Get Image URL Failed %@", error.localizedDescription);
        }];

        [request startAsynchronous];        
    }
        [[NSNotificationCenter defaultCenter] postNotificationName:@"Get Images URL Success" object:self];
}

i want postNotificationName To Call Another Function .

The Problem is NSNotificationCenter, not postNotificationName . Any Help Please ?

Abdullah Umer
  • 4,234
  • 5
  • 36
  • 65
Tefa
  • 21
  • 3
  • 1
    Please write something about where you think your error lies, what your code is supposed to do and any other relevant info – The Unfun Cat Nov 13 '12 at 19:21
  • i have Trips Array with Images URL I wanna Get Image URL By Parsing HTML Page Using TFHpple In Asynchronous Way and i wanna if i get All Images Download PostNotificationCenter – Tefa Nov 13 '12 at 19:27
  • Is there an object that has added itself as an NSNotificationCenter observer for notifications named `"Get Images URL Success"`? – Craig Otis Nov 13 '12 at 19:30
  • no Object Added It Self Craig – Tefa Nov 13 '12 at 19:31

1 Answers1

0

First, you need to add an observer Get_Images_URL_Success

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(gotMessages:) name:Get_Images_URL_Success object:nil];

When you postNotificationName in +GetImagesURL method, this will be called

- (void)gotMessages:(id)sender {
    NSLog(@"This will be called after the postNotificationName");
}

it is recommended you removeObserver when not needed (for example, in dealloc)

[[NSNotificationCenter defaultCenter] removeObserver:self name:Get_Images_URL_Success object:nil];

Now when you postNotificationName like this, gotMessages method should be called

[[NSNotificationCenter defaultCenter] postNotificationName:Get_Images_URL_Success object:self];

[super easy, isn't it?]

Warif Akhand Rishi
  • 23,920
  • 8
  • 80
  • 107