0


我想在 with 中加载一些“图像”(在远程服务器中UIScrollViewNSOperatoinQueue。因为如果我使用 normal 加载它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有一些问题,我无法正确管理。提前致谢。如果您需要更多信息,我将在此处附上。祝你今天过得愉快。

4

1 回答 1

0

不确定这是您的问题还是您的解决方案,如果不测试自己就很难判断。

取自RayWenderlich

addOperationWithBlock:如果你有一个不需要子类化的简单操作,你可以使用block 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 回答