我有一個UIScrollView,我正在加載一些圖像。有時我應用效果的圖像,它需要一點做預加載,所以我決定在不同的線程上使用detachNewThreadSelector 。我正在使用gitHub上的KTPhotoBrowser。iOS detachNewThreadSelector泄漏
所以基本上,我有這樣的功能。
- (void)setCurrentIndex:(NSNumber *)newIndex
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
currentIndex_ = [newIndex integerValue];
[self loadPhoto:currentIndex_];
[self loadPhoto:currentIndex_ + 1];
[self loadPhoto:currentIndex_ - 1];
[self unloadPhoto:currentIndex_ + 2];
[self unloadPhoto:currentIndex_ - 2];
[self setTitleWithCurrentPhotoIndex];
[self toggleNavButtons];
[pool release];
}
我把這個使用
[NSThread detachNewThreadSelector:@selector(setCurrentIndex:) toTarget:self withObject:[NSNumber numberWithInt:5]];
當我運行此,這似乎是扔泄漏。我開始懷疑我是否應該在LoadPhoto方法的代碼周圍放置AutoRelease池。如果你對這段代碼感興趣,我已經將它包含在下面。
- (void)loadPhoto:(NSInteger)index
{
if (index < 0 || index >= photoCount_) {
return;
}
id currentPhotoView = [photoViews_ objectAtIndex:index];
if (NO == [currentPhotoView isKindOfClass:[KTPhotoView class]]) {
// Load the photo view.
CGRect frame = [self frameForPageAtIndex:index];
KTPhotoView *photoView = [[KTPhotoView alloc] initWithFrame:frame];
[photoView setScroller:self];
[photoView setIndex:index];
[photoView setBackgroundColor:[UIColor clearColor]];
// Set the photo image.
if (dataSource_) {
if ([dataSource_ respondsToSelector:@selector(imageAtIndex:photoView:)] == NO) {
UIImage *image = [dataSource_ imageAtIndex:index];
[photoView setImage:image];
} else {
[dataSource_ imageAtIndex:index photoView:photoView];
}
}
[scrollView_ addSubview:photoView];
[photoViews_ replaceObjectAtIndex:index withObject:photoView];
[photoView release];
} else {
// Turn off zooming.
[currentPhotoView turnOffZoom];
}
}
任何想法將不勝感激。
你能告訴我們究竟是哪種物體泄漏嗎? – 2011-02-23 23:06:17
我將很快通過儀器運行它,看看發生了什麼。我會回報我的結果。 – jabroni 2011-02-23 23:11:40