NSDocumentDirectory加载图像 [英] NSDocumentDirectory loading images

查看:57
本文介绍了NSDocumentDirectory加载图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我能够将文件数组保存在我的NSDocumentDirectory中. 这是我用的:

I was able to save my array of files in my NSDocumentDirectory. Here is what I used:

        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *documentsDir = [paths objectAtIndex:0];
        NSString *savedImagePath = [documentsDir stringByAppendingPathComponent:[NSString stringWithFormat:@"Images%d.png", i]];
         ALAssetRepresentation *rep = [[info objectAtIndex: i] defaultRepresentation];
        UIImage *image = [UIImage imageWithCGImage:[rep fullResolutionImage]];
        //NSData *imageData = UIImagePNGRepresentation(image);
        [UIImageJPEGRepresentation(image, 1.0) writeToFile:savedImagePath atomically:YES];

我的问题是如何将具有这种格式@"Images%d.png"的图像加载到此代码中:

My problem is how to load images with this format@"Images%d.png" into this code:

.h

@property (strong, nonatomic) NSMutableArray *images;

.m

    self.images = ??????

推荐答案

以下是从包中读取图像名称的代码.请将捆绑包路径修改为文档目录路径.代码段将为您提供一系列经过排序的图像名称,也使您免于使用 for循环读取名称.

Here is the code to read the image names from bundle. Please modify bundle path to documents directory path. Code snippet will give you array of sorted image names also save you from using a for-loop to read names.

NSString *bundlePath = [[NSBundle mainBundle] bundlePath];
NSFileManager *fileManager = [NSFileManager defaultManager];
NSArray *dirContents = [fileManager contentsOfDirectoryAtPath:bundlePath error:nil];
NSPredicate *filter = [NSPredicate predicateWithFormat:@"self ENDSWITH '.png'"];
NSArray *unsortedImages = [dirContents filteredArrayUsingPredicate:filter];
NSArray *sortedImages = [unsortedImages 
                     sortedArrayUsingComparator:
                     ^NSComparisonResult (id obj1, id obj2){
                         return [(NSString*)obj1 compare:(NSString*)obj2 
                                                 options:NSNumericSearch];
                    }];

友好的建议:避免将任何可下载的数据/图像存储在 NSDocumentDirectory 目录中,因为应用可能会被拒绝.请改用 NSCachesDirectory .请在苹果计算机上阅读此帖子和相关文档开发人员门户.

Friendly advice: Avoid storing any downloadable data/images in NSDocumentDirectory directory as app may get rejected. Use NSCachesDirectory instead. Please read this post and related documents on Apple's developer portal.

干杯!
阿玛.

Cheers!!
Amar.

这篇关于NSDocumentDirectory加载图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆