我在所有目录中显示图片但是它不显示图片。我将NSLog放在代码中,以便我可以找出哪些代码正常工作,我只在日志中得到“j”。我在日志中看不到“a”。你觉得怎么回事?
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
allImagesArray = [[NSMutableArray alloc] init];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSArray *locations = [[NSArray alloc]initWithObjects:@"Bottoms", @"Dress", @"Coats", @"Others", @"hats", @"Tops",nil ];
NSString *fPath = documentsDirectory;
for(NSString *component in locations)
{
fPath = [fPath stringByAppendingPathComponent:component];
}
NSArray *directoryContent = [[NSFileManager defaultManager] directoryContentsAtPath: fPath];
collectionTrash.delegate =self;
collectionTrash.dataSource=self;
for(NSString *str in directoryContent){
NSLog(@"i");
NSString *finalFilePath = [fPath stringByAppendingPathComponent:str];
NSData *data = [NSData dataWithContentsOfFile:finalFilePath];
if(data)
{
UIImage *image = [UIImage imageWithData:data];
[allImagesArray addObject:image];
NSLog(@"array:%@",[allImagesArray description]);
}}
for(NSString *folder in locations) {
// get the folder contents
for(NSString *file in directoryContent) {
// load the image
}
}}
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
NSLog(@"j");
return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
return [allImagesArray count];
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *reuseID = @"ReuseID";
TrashCell *mycell = (TrashCell *) [collectionView dequeueReusableCellWithReuseIdentifier:reuseID forIndexPath:indexPath];
UIImageView *imageInCell = (UIImageView*)[mycell viewWithTag:1];
imageInCell.image = [allImagesArray objectAtIndex:indexPath.row];
NSLog(@"a");
return mycell;
}
随意更多代码。
答案 0 :(得分:1)
如果你看一下例外,它会非常准确地告诉你错误:
您正在尝试在数组上调用'length'方法,该方法根本不存在。您想在此处使用count
。它不在您发布的代码中 - 所以只需搜索length
,如果项目不是很大,您可能会很容易找到它。
NSString *fPath = [documentsDirectory stringByAppendingPathComponent:locations];
会向您发出警告,因为locations
是一个数组。考虑一下 - 只是没有意义:你想将哪些元素添加到路径中?
在我考虑它时,两个错误可能彼此相关:您只是忽略了fPath的编译时错误 - 或警告 - 现在stringByAppendingPathComponent:
方法调用其参数的长度,这是一个预期NSString
。
底线:不要忽略编译器警告!如果你修复它们,你也可以减少崩溃。