所以我一直在寻找这个问题的解决方案,但似乎找不到任何东西。我有一个数组,我使用下面的方法加载图像路径
- (IBAction)btnPictures:(id)sender {
// Create the next view controller.
ImageGalleryViewController *galleryViewController = [[ImageGalleryViewController alloc] initWithNibName:@"ImageGalleryViewController" bundle:nil];
// Search for paths and get users home directory
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask, YES);
// get path at first index (in case multiple returned
NSString *documentsDirectory = [paths objectAtIndex:0];
NSArray *dirContents = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:documentsDirectory error:nil];
NSArray *files = [dirContents filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"pathExtension IN %@", @"png"]];
// Pass the selected object to the new view controller.
[galleryViewController setImageArray:files];
// Push the view controller.
[self.navigationController pushViewController:galleryViewController animated:YES];
}
然后将该图像数组发送到我的UICollectionViewController
中的下面的方法- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
ImageGalleryViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"ImageGalleryViewCell" forIndexPath:indexPath];
NSLog(@"Image at index %@", [imageArray objectAtIndex:indexPath.row]);
[cell.galleryImage setImage:[UIImage imageWithContentsOfFile:[imageArray objectAtIndex:indexPath.row]]];
[cell setBackgroundColor:[UIColor whiteColor]];
return cell;
}
我验证了objectAtIndex通过我的NSLog返回了一个有效的图像名称,但由于某种原因,该单元格没有显示它。
非常感谢任何帮助,提前谢谢。
答案 0 :(得分:2)
NSFileManager的-contentsOfDirectoryAtPath:
返回的数组不提供完整的文件路径,只提供文件和目录的名称。您必须将数组中的每个值附加到文档目录的路径。
这样的事情:
NSString *documentsDirectoryPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
NSString *imagePath = [documentsDirectoryPath stringByAppendingPathComponent:[imageArray objectAtIndex:indexPath.row]];
UIImage *image = [UIImage imageWithContentsOfFile:imagePath];
答案 1 :(得分:0)
我怀疑,在使用(去队)之前,你没有初始化任何细胞。初始化您的手机,然后重试。祝你好运!
PS:通过初始化我的意思是alloc
init
你的手机。