我有一个应用程序,你可以拍照,一旦它被拍摄它会自动上传到服务器,生成这张照片的拇指。拍照后,我的应用程序返回到UICollectionView。 问题有时候,最后一张图片不会加载拇指,我只有占位符而不是实际图片的拇指。我需要重新加载视图或滚动才能看到它。
这是我的加载功能:
- (void)setCellPhotos:(UIImageView *)avatar avatarUrl:(NSString *)avatarUrl
{
UIApplication* app = [UIApplication sharedApplication];
app.networkActivityIndicatorVisible = YES;
NSURL *url = [NSURL URLWithString:avatarUrl];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
UIImage *img = [UIImage imageNamed:@"square"];
__weak UIImageView *avatarView = avatar;
[avatarView setImageWithURLRequest:request
placeholderImage:img
success:^(NSURLRequest *request, NSHTTPURLResponse *response,
UIImage *image)
{
avatarView.image = image;
UIApplication* app = [UIApplication sharedApplication];
app.networkActivityIndicatorVisible = NO;
}failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error)
{
UIApplication* app = [UIApplication sharedApplication];
app.networkActivityIndicatorVisible = NO;
}];
}
我的细胞功能:
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
NSDictionary *pic = [photosArray objectAtIndex:indexPath.row];
UIImageView *picImageView = (UIImageView *)[cell viewWithTag:100];
[self setCellPhotos:picImageView avatarUrl:[pic objectForKey:@"thumb"]];
return cell;
}
我还有一个代表,在完成图片上传后返回给我,然后我请求获取新图库后跟一个:
[self.collectionView reloadData];
我检查了服务器端,并在向我发送回复之前生成了拇指。
感谢您的帮助
答案 0 :(得分:0)
您的问题是来自NURLRequest的响应发生在后台线程上,并且所有UI更新都需要在主线程上。如果你等待足够长的时间,你可能会发现图片最终会显示出来。
要解决此问题,只需修改返回块以包括在主线程上设置图像,例如;
- (void)setCellPhotos:(UIImageView *)avatar avatarUrl:(NSString *)avatarUrl
{
UIApplication* app = [UIApplication sharedApplication];
app.networkActivityIndicatorVisible = YES;
NSURL *url = [NSURL URLWithString:avatarUrl];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
UIImage *img = [UIImage imageNamed:@"square"];
__weak UIImageView *avatarView = avatar;
[avatarView setImageWithURLRequest:request
placeholderImage:img
success:^(NSURLRequest *request, NSHTTPURLResponse *response,
UIImage *image)
{
dispatch_async(dispatch_get_main_queue(), ^{
avatarView.image = image;
UIApplication* app = [UIApplication sharedApplication];
app.networkActivityIndicatorVisible = NO;
});
}failure:^(NSURLRequest *request, NSHTTPURLRes ponse *response, NSError *error)
{
UIApplication* app = [UIApplication sharedApplication];
app.networkActivityIndicatorVisible = NO;
}];
}
您可能还应修改故障块