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

时间:2015-11-24 11:59:53

标签: ios objective-c uicollectionview uicollectionviewcell nsdocumentdirectory

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

我的问题是,

如何从文档目录更快地将图像加载到 UICollectionView .Present我正在将图像完美地加载到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];

将imagesDirectory文件夹( 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;
 }

任何建议都非常赞赏

1 个答案:

答案 0 :(得分:0)

找到简单的解决方案

更改了此行

NSData *imageData = UIImagePNGRepresentation(image);

NSData *imageData = UIImageJPEGRepresentation(image, 0.0);

并删除了这一行

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