【问题标题】:UICollectionView not showing pictures [duplicate]UICollectionView 不显示图片[重复]
【发布时间】:2013-08-25 07:26:57
【问题描述】:

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

请随意获取更多代码。

【问题讨论】:

  • 你应该修改你原来的问题,而不是接受答案并提出一个新问题。

标签: ios directory save uicollectionview


【解决方案1】:

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

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

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

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

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

【讨论】:

    猜你喜欢
    • 2019-11-16
    • 1970-01-01
    • 2021-10-22
    • 2016-10-28
    • 1970-01-01
    • 2021-11-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多