我正在使用cocos2d
开发一个应用程序,该应用程序获取给定用户的所有Facebook照片,然后在CollectionView
中填充它们。我通过图表查询获取所有照片URL's
并将它们插入数组列表中。
[request startWithCompletionHandler:^(FBRequestConnection *connection,
id result,
NSError *error){
NSArray *resultData = [result objectForKey:@"data"];
for (int i=0; i<100;i++) {
NSString *sourceURL = [[resultData objectAtIndex:0] objectForKey:@"source"];
[_imagesUrlList addObject:sourceURL];
}
UICollectionViewFlowLayout *collectionFlowLayout = [[UICollectionViewFlowLayout alloc]init];
[collectionFlowLayout setSectionInset:UIEdgeInsetsMake(5, 7, 5, 7)];
_collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(_masterView.frame.origin.x, _masterView.frame.origin.y+TOP_MARGIN, _masterView.frame.size.width, _masterView.frame.size.height-TOP_MARGIN) collectionViewLayout:collectionFlowLayout];
[collectionFlowLayout release];
_collectionView.delegate = self;
_collectionView.dataSource = self;
[_collectionView registerClass:[ImageCell class] forCellWithReuseIdentifier:@"cellIdentifier"];
[_collectionView setBackgroundColor:[UIColor colorWithRed:226.f/255.f green:228.f/255.f blue:221.f/255.f alpha:1]];
[_masterView addSubview:_collectionView];
[[LoadingView sharedLoadingView] stopTheLoadingActivityView];
[[LoadingView sharedLoadingView] moveVertically:-30];
[self loadImagesInView];
}];
为了测试大量照片,我在图片网址0处插入了100倍。
之后,使用Facebook请求中的给定URL将图像加载到单独的线程上,因为NSURLConnection
会根据请求阻止该线程。
-(void)loadImagesInView{
dispatch_async( dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
for (int i=0; i<[_imagesUrlList count]; i++) {
if (_backed) {
return;
}
NSLog(@"out");
NSString *sourceURL = [_imagesUrlList objectAtIndex:i];
NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:sourceURL]];
NSLog(@"conn %d",i);
dispatch_async( dispatch_get_main_queue(), ^{
UIImage *image = [UIImage imageWithData:data];
image = [self imageWithImage:image scaledToSize:CGSizeMake(100, 100)];
[_imageViewsList addObject:image];
[_collectionView reloadData];
NSLog(@"finish %d",i);
});
}
});
}
问题是我在视图中加载图像时无法选择图片。在方法完成之前,甚至不会调用didSelect
方法。可能是因为当图像准备插入时我调用了reloadData
。您可以在下面看到选择,删除和填充集合视图的方法。
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = @"cellIdentifier";
ImageCell *cell= (ImageCell*)[collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath];
UIImage *photoImg = [_imageViewsList objectAtIndex:indexPath.row];
[cell.imageView setImage:photoImg];
NSNumber *rowNsNum = [NSNumber numberWithUnsignedInt:indexPath.row];
if ([_selectedCells containsObject:[NSString stringWithFormat:@"%@",rowNsNum]]) {
[cell.layer setBorderColor:[[UIColor colorWithRed:255.f/255.f green:255.f/255.f blue:255.f/255.f alpha:0.5] CGColor]];
}
else {
[cell.layer setBorderColor:[[UIColor colorWithRed:0.f/255.f green:0.f/255.f blue:0.f/255.f alpha:0.5] CGColor]];
}
return cell;
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
ImageCell *imgCell = (ImageCell*)[_collectionView cellForItemAtIndexPath:indexPath];
if ([_selectedCells containsObject:[NSString stringWithFormat:@"%d",[indexPath row]]]) {
[_selectedCells removeObject:[NSString stringWithFormat:@"%d",[indexPath row]]];
[imgCell.layer setBorderColor:[[UIColor colorWithRed:0.f/255.f green:0.f/255.f blue:0.f/255.f alpha:0.5] CGColor]];
}
else {
[_selectedCells addObject:[NSString stringWithFormat:@"%d",[indexPath row]]];
[imgCell.layer setBorderColor:[[UIColor colorWithRed:255.f/255.f green:255.f/255.f blue:255.f/255.f alpha:0.5] CGColor]];
}
}
-(void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath{
ImageCell *imgCell = (ImageCell*)[_collectionView cellForItemAtIndexPath:indexPath];
if ([_selectedCells containsObject:[NSString stringWithFormat:@"%d",[indexPath row]]]) {
[_selectedCells removeObject:[NSString stringWithFormat:@"%d",[indexPath row]]];
[imgCell.layer setBorderColor:[[UIColor colorWithRed:0.f/255.f green:0.f/255.f blue:0.f/255.f alpha:0.5] CGColor]];
}
}
答案 0 :(得分:1)
放置[_collectionView reloadData];在旁边的for循环..
-(void)loadImagesInView{
dispatch_async( dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
for (int i=0; i<[_imagesUrlList count]; i++) {
if (_backed) {
return;
}
NSLog(@"out");
NSString *sourceURL = [_imagesUrlList objectAtIndex:i];
NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:sourceURL]];
NSLog(@"conn %d",i);
dispatch_async( dispatch_get_main_queue(), ^{
UIImage *image = [UIImage imageWithData:data];
image = [self imageWithImage:image scaledToSize:CGSizeMake(100, 100)];
[_imageViewsList addObject:image];
NSLog(@"finish %d",i);
});
}
[_collectionView reloadData];
});
}