46@property (strong, nonatomic) NSMutableArray<NSString *> *
imagePaths;
60 self.imagePaths = [NSMutableArray new];
63 UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
64 layout.itemSize = CGSizeMake(100, 100);
65 layout.minimumLineSpacing = 10;
66 layout.minimumInteritemSpacing = 10;
69 self.collectionView = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:layout];
70 self.collectionView.backgroundColor = [UIColor whiteColor];
71 self.collectionView.dataSource =
self;
72 self.collectionView.delegate =
self;
75 [
self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"ImageCell"];
78 [
self.view addSubview:self.collectionView];
88 NSFileManager *fileManager = [NSFileManager defaultManager];
89 NSString *documentsDirectoryPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
91 NSArray *directoryContents = [fileManager contentsOfDirectoryAtPath:documentsDirectoryPath error:&error];
94 NSLog(
@"Found %lu items in the documents directory.", (
unsigned long)directoryContents.count);
95 for (NSString *fileName in directoryContents) {
96 if ([fileName.pathExtension isEqualToString:
@"png"] || [fileName.pathExtension isEqualToString:
@"jpg"] || [fileName.pathExtension isEqualToString:
@"jpeg"]) {
97 NSString *filePath = [documentsDirectoryPath stringByAppendingPathComponent:fileName];
98 UIImage *image = [UIImage imageWithContentsOfFile:filePath];
100 [
self.fuzzedImages addObject:image];
101 [
self.imagePaths addObject:fileName];
102 NSLog(
@"Successfully loaded image: %@", fileName);
104 NSLog(
@"Failed to load image: %@", fileName);
109 NSLog(
@"Error reading the documents directory: %@", error.localizedDescription);
112 NSLog(
@"Total loaded fuzzed images: %lu", (
unsigned long)
self.
fuzzedImages.count);
115 dispatch_async(dispatch_get_main_queue(), ^{
116 [
self.collectionView reloadData];
120#pragma mark - UICollectionViewDataSource
128- (NSInteger)
collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
129 return self.fuzzedImages.count;
138- (UICollectionViewCell *)
collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
139 UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"ImageCell" forIndexPath:indexPath];
142 cell.backgroundColor = [UIColor lightGrayColor];
144 UIImageView *imageView = [[UIImageView alloc] initWithFrame:cell.contentView.bounds];
145 imageView.contentMode = UIViewContentModeScaleAspectFit;
146 imageView.image =
self.fuzzedImages[indexPath.row];
147 [cell.contentView addSubview:imageView];
149 NSLog(
@"Configuring cell for item at %@ with image name: %@", indexPath,
self.
imagePaths[indexPath.row]);