-1

我在所有目录中显示图片但是它不显示图片。我将 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;
    }

随意获取更多代码。

4

1 回答 1

1

如果您查看异常,它会非常准确地告诉您出了什么问题:

您正在尝试在根本不存在的数组上调用“长度”方法。你想在count这里使用。不过,它不在您发布的代码中 - 所以只需搜索一下,length如果项目不大,您可能会很容易找到它。

NSString *fPath = [documentsDirectory stringByAppendingPathComponent:locations];给你一个警告,因为locations是一个数组。想一想 - 只是没有意义:您想将哪些元素添加到路径中?

在我看来,这两个错误可能彼此相关:您只是忽略了 fPath 的编译时错误或警告,现在该stringByAppendingPathComponent:方法在其参数上调用 length,这是预期的NSString.

底线:不要忽略编译器警告!如果你解决了这些问题,你可能也会减少崩溃。

于 2013-08-25T08:00:37.947 回答