NSDocumentDirectory加载图片

时间:2012-05-29 03:55:39

标签: iphone ios xcode ipad

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

        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

@property (strong, nonatomic) NSMutableArray *images;

的.m

    self.images = ??????

3 个答案:

答案 0 :(得分:1)

将图像保存到NSDocumentDirectory

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];

用于检索图像

          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 :(得分:0)

以下是从bundle中读取图像名称的代码。请修改文件目录路径的包路径。代码段将为您提供已排序的图像名称数组,还可以避免使用 for-loop 来读取名称。

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 。请阅读Apple的开发人员门户网站上的post及相关文档。

干杯!!
阿玛尔。

答案 2 :(得分:0)

您正在将png扩展名保存为jpg文件。我相信这应该有效:

    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];