JSA986
JSA986

Reputation: 5936

Get images from documents directory to collection view

How can I get images from documents directory to poplulate a collection view. So far I get all the images dumped into each cell according to my log, (or at least the image name is printed in the log)

First im getting the image names from the documents directory filelist is an NSMutable array of imageNames.png

 NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsPath = [paths objectAtIndex:0];
    NSArray *dirContents = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:documentsPath error:nil];
    fileList=[[NSMutableArray alloc]init];
    for (NSString *filename in dirContents) {
        NSString *fileExt = [filename pathExtension];

        if ([fileExt isEqualToString:@"png"]) {

            [fileList addObject:filename];
        }
    }
    NSLog(@"document folder content list %@ ",fileList);

This returns a list of my png file names in my NSMutsableArray fileList. Then I want to get all these images into my collection view

//set up cell from nib in viewDidLoad
    UINib *cellNib = [UINib nibWithNibName:@"NibCell" bundle:nil];
    [self.appliancesCollectionView registerNib:cellNib forCellWithReuseIdentifier:@"cvCell"];


     /////


-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {

return 1;
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {


return fileList.count;
NSLog(@"collection view count is %@",fileList);

}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {

// Setup cell identifier
static NSString *cellIdentifier = @"cvCell";

UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath];


NSString *fileName = [fileList objectAtIndex:indexPath.row];
cell.backgroundView = [[UIImageView alloc] initWithImage: [UIImage imageNamed: fileName]];
NSLog(@"cell Bg image %@",fileList);

return cell;

}


The probelm is nothing shows up in my collection view cells

Upvotes: 2

Views: 1533

Answers (2)

Avt
Avt

Reputation: 17053

The problem is that contentsOfDirectoryAtPath returns relative file paths. And you need absolute. Something like following should be used:

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsPath = [paths objectAtIndex:0];
NSArray *dirContents = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:documentsPath error:nil];
fileList=[[NSMutableArray alloc]init];

NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]; // NEW LINE 1

for (NSString *filename in dirContents) {
    NSString *fileExt = [filename pathExtension];
    if ([fileExt isEqualToString:@"png"]) {
        NSString *fullPath = [documentsDirectory stringByAppendingPathComponent:filename]; // NEW LINE 2
        [fileList addObject:fullPath]; // NEW LINE 3
    }
}
NSLog(@"document folder content list %@ ",fileList);

Upvotes: 2

Mykola Denysyuk
Mykola Denysyuk

Reputation: 1985

You need to use imageWithContentsOfFile: instead imageNamed:

NSString * filePath = [[NSBundle mainBundle] pathForResource:<imageNameWithoutExtansion>ofType:<fileExtansion>];
cell.backgroundView = [[UIImageView alloc] initWithImage: [UIImage imageWithContentsOfFile: filePath]];

Upvotes: 1

Related Questions