0

我能够将我的文件数组保存在我的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 = ??????
4

3 回答 3

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];
于 2013-09-26T10:35:34.427 回答
0

这是从 bundle 中读取图像名称的代码。请将捆绑路径修改为文档目录路径。代码片段将为您提供排序图像名称的数组,也使您免于使用for 循环来读取名称。

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 开发者门户上的这篇文章和相关文档。

干杯!!
阿马尔。

于 2012-05-29T06:35:13.050 回答
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];
于 2012-05-30T16:48:47.887 回答