如何从 ios 中的文档目录更快地加载图像

how to load images faster from document directory in ios

我知道有很多与我的主题相关的答案,但没有解决我的问题。

我的问题是,

如何从 文档目录 中更快地将 images 加载到 UICollectionView 中。现在我是将图像完美地加载到 UICollectionView 中,但图像加载速度非常缓慢

我的代码如下

正在将图像保存到文档目录文件夹中(Chandan)

UIImage *image = info[UIImagePickerControllerOriginalImage];


NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentDirectory = paths.firstObject;
NSData *imageData = UIImagePNGRepresentation(image);

NSString *imageFolder = @"Chandan";


NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"yyyyMMddHHmmss"];
NSDate *now = [NSDate date];
NSString *theDate = [dateFormat stringFromDate:now];
NSString *myUniqueName = [NSString stringWithFormat:@"Photo-%@.png",theDate];

NSString *imagePath = [NSString stringWithFormat:@"%@/%@",documentDirectory,imageFolder];

BOOL isDir;
NSFileManager *fileManager= [NSFileManager defaultManager];
if(![fileManager fileExistsAtPath:imagePath isDirectory:&isDir])
    if(![fileManager createDirectoryAtPath:imagePath withIntermediateDirectories:YES attributes:nil error:NULL])
        NSLog(@"Error: folder creation failed %@", documentDirectory);

[[NSFileManager defaultManager] createFileAtPath:[NSString stringWithFormat:@"%@/%@", imagePath, myUniqueName] contents:nil attributes:nil];
[imageData writeToFile:[NSString stringWithFormat:@"%@/%@", imagePath, myUniqueName] atomically:YES];

并将图像从 documentDirectory 文件夹 (Chandan) 加载到数组中

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentDirectory = paths.firstObject;
NSString *fullPath = [NSString stringWithFormat:@"%@/%@", documentDirectory, @"Chandan"]; // image path is same as above
NSArray *dirContents = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:fullPath error:nil];

fileList=[[NSMutableArray alloc]init];
fileName=[[NSMutableArray alloc]init];

if ([[NSFileManager defaultManager] fileExistsAtPath:fullPath]) {

    for (NSString *filename in dirContents) {
        NSString *fileExt = [filename pathExtension];
        if ([fileExt isEqualToString:@"png"]) {
            NSString *fullPth = [fullPath stringByAppendingPathComponent:filename];
            [fileList addObject:fullPth];
            [fileName addObject:filename];
            NSLog(@"File name : %@ ",filename);
            NSLog(@"fullPth : %@ ",fullPth);

        }
    }


} else {

    NSLog(@"No Files");
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {

return fileList.count;
 }

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

ImageCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];

cell.imgView.image = nil;


NSString *filePath = [fileList objectAtIndex:indexPath.row];

 UIImage *image =[UIImage imageWithContentsOfFile:filePath];

//--asynchro--//

dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul);
dispatch_async(queue, ^{



    dispatch_async(dispatch_get_main_queue(), ^{
        if (image) {

             UIImage *imageToDisplay =[UIImage imageWithCGImage:[image CGImage]
                                             scale:1.0
                                       orientation: UIImageOrientationRight];

            cell.imgView.image = imageToDisplay;

          [activityIndicator stopAnimating];
           activityIndicator.hidden = YES;
           CollectionView.hidden = NO;


        } else {
            cell.imgView.image = [UIImage imageNamed:@"tours-walk.png"];
        }
    });
});


//--//   

cell.selectedLabel.backgroundColor = [UIColor clearColor];

// Configure the cell

cell.layer.shouldRasterize = true;
cell.layer.rasterizationScale = [UIScreen mainScreen].scale;


return cell;
 }

任何建议都非常赞赏

找到简单的解决方案

更改了这一行

NSData *imageData = UIImagePNGRepresentation(image);

进入

NSData *imageData = UIImageJPEGRepresentation(image, 0.0);

并且还删除了这一行

UIImage *imageToDisplay =[UIImage imageWithCGImage:[image CGImage]
                                         scale:1.0
                                   orientation: UIImageOrientationRight];