2013-11-26 63 views
0

所以我一直在寻找解决这个问题,但似乎无法找到任何东西。我有一个数组,我使用下面的方法图像没有显示在我的UICollectionView

- (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

尝试改变'ImageGalleryViewCell'作为返回类型在'的CollectionView:cellForItemAtIndexPath()'方法。可能是有效的。 –

回答

2

NSFileManager的-contentsOfDirectoryAtPath:返回的数组不提供完整的文件路径,而只是文件和目录的名称。您必须将数组中的每个值附加到文档目录的路径。

像这样:

NSString *documentsDirectoryPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"]; 
NSString *imagePath = [documentsDirectoryPath stringByAppendingPathComponent:[imageArray objectAtIndex:indexPath.row]]; 
UIImage *image = [UIImage imageWithContentsOfFile:imagePath]; 
+0

这就是它,对于菜鸟的错误感到抱歉:)再次感谢! – DerekH

+0

没问题。我曾经经历过这个问题。 :) –

0

我怀疑,在使用(取代)一个之前,您没有初始化任何单元。初始化您的单元格,然后重试。祝你好运! PS:通过初始化我的意思是,allocinit你的手机。