I have an application that uploads several thumbnails to UIScrollVIEW. This is a lengthy operation, and in order not to block the display of the rest of the user interface, I run it in a separate thread. This works the first time the application starts, but later a new set of images is loaded into UIScrollView. When I disconnect the stream, the second time the application crashes (sometimes). Code follows:
[NSThread detachNewThreadSelector:@selector(addThumbnailsToScrollView) toTarget:self withObject:nil];
- (void) addThumbnailsToScrollView {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
float xPosition = THUMB_H_PADDING;
int pageIndex = 0;
for (Page *page in self.pages) {
NSString *name = [page valueForKey:@"pageBackground"];
NSString *basePath = [[NSBundle mainBundle] pathForResource:page.pageBackground ofType:@"jpg" inDirectory:nil];
UIImage *thumbImage = [UIImage imageWithContentsOfFile:basePath];
thumbImage = [thumbImage imageScaledToSize:CGSizeMake(80, 100)];
if (thumbImage) {
ThumbImageView *thumbView = [[ThumbImageView alloc] initWithImage:thumbImage];
[thumbView setDelegate:self];
[thumbView setImageName:name];
[thumbView setImageSize:CGSizeMake(80, 100)];
[thumbView setPageIndex:pageIndex];
pageIndex ++;
CGRect frame = [thumbView frame];
frame.origin.y = 0;
frame.origin.x = xPosition;
[thumbView setFrame:frame];
[thumbnailPagesScrollView addSubview:thumbView];
[thumbView release];
xPosition += (frame.size.width + THUMB_H_PADDING);
}
}
[self hightlightThumbnailPageAtIndex:0];
[(UIActivityIndicatorView *)[thumbnailPagesScrollView.superview viewWithTag:100] stopAnimating];
[pool release];
}
I thought that a separate thread would exit as soon as the main login procedure was completed. Was the second call to detach a thread a new thread? Why does the application crash, but sometimes not?
thank
Jk
source
share