首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >NSDocumentDirectory加载图像

NSDocumentDirectory加载图像
EN

Stack Overflow用户
提问于 2012-05-29 11:55:39
回答 3查看 2.1K关注 0票数 0

我能够将我的文件数组保存在我的NSDocumentDirectory中。下面是我使用的代码:

代码语言:javascript
复制
        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"图像加载到下面的代码中:

.h

代码语言:javascript
复制
@property (strong, nonatomic) NSMutableArray *images;

.m

代码语言:javascript
复制
    self.images = ??????
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2012-05-29 14:35:13

下面是从bundle中读取图像名称的代码。请将捆绑包路径修改为文档目录路径。代码片段将为您提供已排序的图像名称数组,并使您不必使用for循环来读取名称。

代码语言:javascript
复制
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。请在苹果的开发者门户上阅读此post和相关文档。

干杯!!

阿马尔。

票数 0
EN

Stack Overflow用户

发布于 2013-09-26 18:35:34

用于将图像保存到NSDocumentDirectory

代码语言:javascript
复制
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
        NSString *documentsDirectory = [paths objectAtIndex:0];//m_nImageIndex
        NSString *imagename = [NSString stringWithFormat:@"savedImage%d.png",m_nImageIndex];
        NSString *savedImagePath = [documentsDirectory stringByAppendingPathComponent:imagename];
        // Get PNG data from following method
        NSData *myData =     UIImagePNGRepresentation(image);
        // It is better to get JPEG data because jpeg data will store the location and other related information of image.
        [myData writeToFile:savedImagePath atomically:NO];
        //[UIImageJPEGRepresentation(image ,1) writeToFile:savedImagePath atomically:NO];

用于检索图像

代码语言:javascript
复制
          NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,     NSUserDomainMask, YES);
          NSString *documentsDirectory = [paths objectAtIndex:0];
          NSString *getImagePath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"savedImage%d.png",indexPath.row]];
          UIImage *img = [UIImage imageWithContentsOfFile:getImagePath];

          cell.backgroundView = [[UIImageView alloc] initWithImage:img];
票数 1
EN

Stack Overflow用户

发布于 2012-05-31 00:48:48

您正在将png扩展名另存为jpg文件。我相信这应该是可行的:

代码语言:javascript
复制
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDir = [paths objectAtIndex:0];
    NSString *savedImagePath = [documentsDir stringByAppendingPathComponent:[NSString stringWithFormat:@"Images%d.jpg", i]];
    ALAssetRepresentation *rep = [[info objectAtIndex: i] defaultRepresentation];
    UIImage *image = [UIImage imageWithCGImage:[rep fullResolutionImage]];
    [UIImageJPEGRepresentation(image, 1.0) writeToFile:savedImagePath atomically:YES];

    // to load the image later:
    UIImage *imageFromDisk = [UIImage imageWithContentsOfFile:savedImagePath];
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10792873

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档