Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/95.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/39.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 图像未显示在我的UICollectionView中_Ios_Iphone_Objective C_Uiimageview_Uicollectionview - Fatal编程技术网

Ios 图像未显示在我的UICollectionView中

Ios 图像未显示在我的UICollectionView中,ios,iphone,objective-c,uiimageview,uicollectionview,Ios,Iphone,Objective C,Uiimageview,Uicollectionview,所以我一直在寻找解决这个问题的方法,但似乎什么都找不到。我有一个数组,我使用下面的方法加载图像路径 - (IBAction)btnPictures:(id)sender { // Create the next view controller. ImageGalleryViewController *galleryViewController = [[ImageGalleryViewController alloc] initWithNibName:@"ImageGalleryViewCont

所以我一直在寻找解决这个问题的方法,但似乎什么都找不到。我有一个数组,我使用下面的方法加载图像路径

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

}
然后将该图像数组发送到my 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是否返回了有效的图像名称,但由于某些原因,单元格未显示该名称


非常感谢您的帮助,提前谢谢。

我想您在使用(取消)单元格之前没有初始化任何单元格。初始化您的手机,然后重试。祝你好运


PS:通过初始化,我的意思是,
alloc
init
您的单元格。

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

大概是这样的:

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

尝试将
ImageGalleryViewCell
更改为
collectionView:cellForItemAtIndexPath()方法中的返回类型。也许它是有效的。就是这样,为新手的错误道歉:)再次感谢!没问题。这个问题我自己也经历过一次。:)