【发布时间】:2013-11-26 07:08:35
【问题描述】:
所以我一直在寻找解决此问题的方法,但似乎找不到任何东西。我有一个数组,我使用以下方法加载图像路径
- (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;
}
我通过我的 NSLog 验证了 objectAtIndex 正在返回一个有效的图像名称,但由于某种原因,单元格没有显示它。
非常感谢任何帮助,在此先感谢。
【问题讨论】:
-
尝试将
ImageGalleryViewCell更改为collectionView: cellForItemAtIndexPath()方法中的返回类型。也许它有效。
标签: ios iphone objective-c uiimageview uicollectionview