I want to load some "image" (In remote server) in a UIScrollView with NSOperatoinQueue. Because If I load it with normal NSURL, NSData or with NSMutableURLRequest it takes too much time to load for all the images. After that I show those images in UIButton. Here is my code:

- (void)viewDidLoad
    [super viewDidLoad];
    [self startAnimation:nil];

    self.imageDownloadingQueue = [[NSOperationQueue alloc] init];
    self.imageDownloadingQueue.maxConcurrentOperationCount = 4; // many servers limit how many concurrent requests they'll accept from a device, so make sure to set this accordingly
    self.imageCache = [[NSCache alloc] init];

    [self performSelector:@selector(loadData) withObject:nil afterDelay:0.5];

-(void) loadData
    adParser = [[AdParser alloc] loadXMLByURL:getXMLURL];
    adsListArray = [adParser ads];
    displayArray = [[NSMutableArray alloc] init];

    for (AdInfo *adInfo1 in adsListArray)
        AdInfo *adInfo2 = [[AdInfo alloc] init];
        [adInfo2 setBannerIconURL:adInfo1.bannerIconURL];
        [adInfo2 setBannerIconLink:adInfo1.bannerIconLink];
        [displayArray addObject:adInfo2];
    [self loadScrollView];
    [activityIndicator stopAnimating];

-(void) loadScrollView
    [self.scrollView setScrollEnabled:YES];
    [self.scrollView setContentSize:CGSizeMake([displayArray count] * ScrollerWidth, ScrollerHight)];

    for (int i = 0; i < [displayArray count]; i++)
        adButtonOutLet = [[UIButton alloc] initWithFrame:CGRectMake(i*320, 0, ButtonWidth, ButtonHight)];
        currentAd = [displayArray objectAtIndex:i];

        NSString *imageUrlString = [currentAd bannerIconURL];
        UIImage *cachedImage = [self.imageCache objectForKey:imageUrlString];
        if (cachedImage)
            [adButtonOutLet setImage:cachedImage forState:UIControlStateNormal];
            [self.imageDownloadingQueue addOperationWithBlock:^
                 NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:imageUrlString]];
                 UIImage *image    = nil;
                 image = [UIImage imageWithData:imageData];
                 // add the image to your cache
                 [self.imageCache setObject:image forKey:imageUrlString];
                 // finally, update the user interface in the main queue
                 [[NSOperationQueue mainQueue] addOperationWithBlock:^
                      [adButtonOutLet setImage:image forState:UIControlStateNormal];

        adButtonOutLet.userInteractionEnabled= YES;
        [adButtonOutLet setTag:i];
        [adButtonOutLet addTarget:self action:@selector(goToURL:) forControlEvents:UIControlEventTouchUpInside];

        [self.scrollView addSubview:adButtonOutLet];

Can anyone tell me what's wrong with the above code? There is no problem of parsing or retrieving data from Remote server. I check it by NSLog. I think the NSOperationQueue have some problem, which I can't manage properly. Thanks in advance. If you needed more information, I will attach here. Have a nice day.

Related posts

Recent Viewed