0


UIScrollViewwith に「イメージ」(リモートサーバー内)をロードしたいNSOperatoinQueue。通常でロードするとNSURLNSDataまたはNSMutableURLRequestすべての画像をロードするのに時間がかかりすぎるためです。その後、それらの画像を に示しますUIButton。これが私のコードです:

- (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];
        }
        else
        {
            [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];
    }
}

上記のコードの何が問題なのか、誰か教えてもらえますか? リモート サーバーからのデータの解析または取得に問題はありません。で確認しNSLogます。NSOperationQueue適切に管理できない問題があると思います。前もって感謝します。さらに情報が必要な場合は、ここに添付します。良い1日を。

4

1 に答える 1

0

これがあなたの問題なのか解決策なのかわからないので、自分でテストしないとわかりません。

RayWenderlichから取得

addOperationWithBlock: サブクラス化する必要のない単純な操作がある場合は、ブロック API を使用して操作を作成できます。ブロック内の外部から任意のオブジェクトを参照する場合は、弱い参照を渡す必要があることに注意してください。また、ブロック内の UI に関連する何かを実行する場合は、メイン スレッドで実行する必要があります。

// Create a weak reference
__weak MyViewController *weakSelf = self;

// Add an operation as a block to a queue
[myQueue addOperationWithBlock: ^ {

    NSURL *aURL = [NSURL URLWithString:@"http://www.somewhere.com/image.png"];
    NSError *error = nil;
    NSData *data = [NSData dataWithContentsOfURL:aURL options:nil error:&error];
    UIImage *image = nil;
    If (data)
        image = [UIImage imageWithData:data];

    // Update UI on the main thread.
    [[NSOperationQueue mainQueue] addOperationWithBlock: ^ {
        weakSelf.imageView.image = image;
    }];

}];
于 2014-09-08T09:58:02.000 に答える