嗨,我正在使用PST Collectionview
类似UIcollectionview
的。我想将图像从我的文档目录加载到集合视图。第一次尝试同步方式但它太慢了..所以知道如何将图像异步加载到集合视图。
在我的viewdidload中,我添加了以下代码,因此它将图像下载到我的文档目录
dispatch_queue_t imageLoadQueue = dispatch_queue_create("com.aaa.nkp",NULL);
dispatch_async(imageLoadQueue, ^{
usleep(1000000);
docPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
for(int i=0; i <[Images count] ;i++){
imgURL = [Images objectAtIndex:i];
[imagePreview addObject:imgURL];
imgData=[NSData dataWithContentsOfURL:[NSURL URLWithString:imgURL]];
[imgData writeToFile:[NSString stringWithFormat:@"%@/%@", docPath, [imgURL lastPathComponent]] atomically:YES];
}
[[self collectionView] reloadData];
});
和内部collectionview cellforitem() 方法
- (PSTCollectionViewCell *)collectionView:(PSTCollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
cell = nil;
cell = (GMMCollectionViewCell *)[self.collectionView dequeueReusableCellWithReuseIdentifier:@"test" forIndexPath:indexPath];
cell.tag = indexPath.row;
docPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
BOOL isImageLoaded = YES;
bookImage = [UIImage imageWithContentsOfFile:[NSString stringWithFormat:@"%@/%@", docPath, [[Images objectAtIndex:indexPath.row] lastPathComponent]]];
if(bookImage == nil)
isImageLoaded = NO;
if(!isImageLoaded){
[[cell grid_image] setImage:[UIImage imageNamed:@"Placeholder.png"]];
}
else{
[[cell grid_image] setImage:bookImage ];
}
return cell;
}