NSThread 延迟加载图片列表
从Documents目录下读取图片列表时,有加载延迟,界面有一个空白期。我想办法默认显示空图片占位(显示一个默认灰边图片),然后使用NSThread加载真正的图片
UIImageView 添加在UIScrollView中 _myScrollView,3步走:
for(int i=0;i<10;i++){ UIImageView *iv = [[UIImageViewalloc] initWithFrame:CGRectMake(0,i*110,320,300)]; iv.image = [UIImageimageNamed:@"defautlImage"]; [_myScrollView addSubview:iv]; //生成线程并启用它 [NSThreaddetachNewThreadSelector:@selector(loadPictures:) toTarget:selfwithObject:nil]; }
//加载图片 -(void)loadPictures:(id)sender{ int len = [_myScrollView.subviews count]; int i=0; for (;i<len;i++) { UIView *subview = [_myScrollView.subviews objectAtIndex:i]; if ([subview isKindOfClass:[UIImageView class]]) { [self performSelectorOnMainThread:@selector(show:) withObject:[NSNumber numberWithInt:i] waitUntilDone:YES]; } } }
//更新图片 -(void)show:(NSNumber*)index{ UIImageView *iv = (UIImageView*)[_myScrollView.subviews objectAtIndex:[index intValue]]; UIImage *image = [UIImage imageWithContentsOfFile:------];//------是图片路径 iv.image = image; iv.frame = ------;//-----重新绘制UIImageView的frame iv.userInteractionEnabled = YES; //单击imageview UITapGestureRecognizer *singleTouch = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(singleTouch:)]; [iv addGestureRecognizer:singleTouch]; }