在iPad编程中使用多线程时出现问题(用于加载大量图像)

时间:2011-06-03 10:58:03

标签: iphone multithreading image ipad autorelease

我应该将大量图像加载到滚动视图上,我想这需要一些时间和记忆。所以,我使用一个单独的线程在后台加载这些图像。我使用了以下多线程方法

[NSThread detachNewThreadSelector:@selector(prepareSliderView) toTarget:self withObject:nil];

将图像加载到单独的线程上。在实现此方法时,我在GDB中收到以下消息

NSAutoreleaseNoPool(): Object 0x10647180 of class NSPathStore2 autoreleased with no pool in place - just leaking
NSAutoreleaseNoPool(): Object 0x10647300 of class NSPathStore2 autoreleased with no pool in place - just leaking
NSAutoreleaseNoPool(): Object 0x10646a00 of class NSCFString autoreleased with no pool in place - just leaking
NSAutoreleaseNoPool(): Object 0x10647480 of class NSPathStore2 autoreleased with no pool in place - just leaking
NSAutoreleaseNoPool(): Object 0x10646a20 of class UIImage autoreleased with no pool in place - just leaking

我不知道这意味着什么,因为我之前从未执行过此方法。所以,我的问题是

1 - 我应该使用一些自动释放池以及此方法

2 - 有没有其他方法可以加载大量图像而不会给主线加载大量负载

-(IBAction)prepareSliderView{
NSLog(@"Preparing slider view");

NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

int totalPages=[kbDataSource numberOfPagesInBook];
sliderScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, self.view.frame.size.height-300, self.view.frame.size.width,200 )];
sliderScrollView.contentSize = CGSizeMake(totalPages*150+((totalPages-1)*10), 200); 
sliderScrollView.backgroundColor=[[UIColor blackColor] colorWithAlphaComponent:1.0];
thumbnailContentView = [[UIView alloc] initWithFrame:CGRectMake(10, 0, sliderScrollView.contentSize.width, sliderScrollView.contentSize.height)];

newPageSlider.continuous=YES;
newPageSlider.minimumValue=1;
newPageSlider.maximumValue=totalPages;

 thumbnailFrame = CGRectMake(0, 25, 120, 150);
 pageNumFieldFrame = CGRectMake(0, thumbnailFrame.size.height+10, thumbnailFrame.size.width, 50);
for(int i=1;i<totalPages;i++){
    thumbnailView = [[UIView alloc] initWithFrame:thumbnailFrame];
    thumbnailView.backgroundColor=[UIColor clearColor];
    UIButton *thumbnail = [[UIButton alloc] initWithFrame:thumbnailFrame];
    UILabel *pageNumField = [[UILabel alloc] initWithFrame:pageNumFieldFrame];
    pageNumField.backgroundColor=[UIColor clearColor];
    thumbnail.tag=i+1;
    thumbnailView.tag=i+1;

    id<PageDataSource> pd = [kbDataSource pageAtIndex:i];

    thumbnailFrame = CGRectMake(thumbnailFrame.origin.x+150+10, thumbnailFrame.origin.y, 120, 150);
    pageNumFieldFrame = CGRectMake(pageNumFieldFrame.origin.x+150+10, pageNumFieldFrame.origin.y, thumbnailFrame.size.width, 50);
    [thumbnail setBackgroundImage:[pd thumbnailImageForPageNumber:i] forState:UIControlStateNormal];
    pageNumField.text =[NSString stringWithFormat:@"Page %d",i];
    pageNumField.textColor=[UIColor whiteColor];
    [thumbnailContentView addSubview:thumbnailView];
    [sliderScrollView addSubview:pageNumField];
    [sliderScrollView addSubview:thumbnail];
    [sliderScrollView addSubview:thumbnailView];
    [sliderScrollView bringSubviewToFront:thumbnail];
    [sliderScrollView bringSubviewToFront:pageNumField];
    [self.view bringSubviewToFront:thumbnail];
    [thumbnailView release];
    [pageNumField release];
    [thumbnail release];
    [thumbnail addTarget:self action:@selector(navigateToPage:) forControlEvents:UIControlEventTouchUpInside];
    [pool release];
}

}

2 个答案:

答案 0 :(得分:3)

对于您在新线程中执行的每个 main 方法,无论是通过NSThread还是通过performsSelectorInBackground:,您都必须创建一个自动释放池,如下所示:

- (void)myBackgroundOperation
{
   NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

   // Do some work.

   // At the very end, release the pool.
   [pool release];
}

另请参阅:Why is there no autorelease pool when I do performSelectorInBackground

但是对于你的任务,我建议你也查看NSOperationQueue。例如,请参阅文章Cocoa Tutorial: NSOperation and NSOperationQueue

答案 1 :(得分:1)

作为documentation says

  

aSelector负责的方法   用于设置自动释放池   新分离的线程和释放   退出之前的那个池

因此,在您的prepareSliderView方法中,您需要创建并释放自动释放池:

NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

// some work

[pool release];