将图像路径保存到NSDocuments然后将其检索为UIImage

时间:2018-01-26 13:42:33

标签: ios objective-c uiimageview nsfilemanager

我有一个图像选择控制器,可以将选定的图像存储到目录中。我能够保存路径,但检索它们会导致崩溃。我不确定要保存的代码是错误还是要检索的代码是错误的。这是崩溃错误。

*** Terminating app due to uncaught exception 
'NSInvalidArgumentException', reason: '-[__NSCFString size]: unrecognized 
selector sent to instance 0x1c484bc40'

以下是检索文件并存储在NSMutableArray中的代码。这是崩溃发生的地方。我需要路径的结果为UIImage个文件。

- (void)loadCustomDesignGIFEdit:(TSPEventRepository *)record {
    NSError *error = nil;
    NSMutableArray *arrSlidshowImg = @[].mutableCopy;
    NSString *Dir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
    NSUserDomainMask, YES) objectAtIndex:0];
    NSString *path = [NSString stringWithFormat:@"%@%@",Dir, @"/Images/"];
    NSArray * directoryContents = [[NSFileManager defaultManager]
     contentsOfDirectoryAtPath:path error:&error];
    UIImage *image = [UIImage imageWithContentsOfFile:path];

    for (image in directoryContents) {
        [arrSlidshowImg addObject:image];
    }
    NSLog(@"ARRAY RESULT: %@", arrSlidshowImg);

    fourBySixBackgroundImageViewGIF.image = [arrSlidshowImg objectAtIndex:0];      
}

当我按下按钮拨打loadCustomDesignGIFEdit时,日志会显示我选择的图像的正确路径,但它会崩溃。我需要它将路径保存为带有PNG表示的图像到NSMutableArray

这是日志结果:

ARRAY RESULT: (
    "picName_IMG_7293.JPG",
    "picName_IMG_7294.JPG"
)

1 个答案:

答案 0 :(得分:0)

有一句话说:

UIImage *image = [UIImage imageWithContentsOfFile:path]

这没有意义,因为path是文件夹的路径,而不是任何特定图像的路径。

但是你的神秘错误源于以下代码:

fourBySixBackgroundImageViewGIF.image = [arrSlidshowImg objectAtIndex:0];

不幸的是,arrSlidshowImg是一个字符串数组,而不是一个图像数组。因此,当您以image的{​​{1}}提供该错误时,错误会以密码告知您它正在尝试检索UIImageView图片,但是size您提供没有这样的方法/财产。

你可能意味着:

NSString