0

我正在尝试更改保存图像的文件夹路径。(从 myfolder 到 yourfolder)
我编写的代码如下所示。

当我执行以下代码时,会发生错误。我认为它试图在更改之前从文件夹加载。

如何从更改的文件夹中加载图像文件?请帮忙。:o

// image loading.
UIImage *image1 = [UIImage imageNamed:@"alarm.png"];
UIImageView *imageView1 = [[UIImageView alloc] initWithImage:image1];
imageView1.center = CGPointMake(100, 100);
[self.view addSubview:imageView1];

// get document path.
NSFileManager *fileManager = [NSFileManager defaultManager];
NSArray *appsDirectory = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentPath = [appsDirectory objectAtIndex:0];

// create myFolder path.
NSString *myFolderPath = [documentPath stringByAppendingPathComponent:@"myFolder"];
if ([fileManager createDirectoryAtPath:myFolderPath withIntermediateDirectories:YES attributes:nil error:nil]){
    NSLog(@"success");
}
else {
    NSLog(@"failed");
}

// write image to saveImage@2x.png file.
NSData *imageData = [NSData dataWithData:UIImagePNGRepresentation(image1)];

NSString *saveName = @"saveImage@2x.png";
NSString *savePath = [myFolderPath stringByAppendingPathComponent:saveName];
[imageData writeToFile:savePath atomically:YES];


// load from saved the image1 to image2
UIImage *image2 = [UIImage imageWithContentsOfFile:savePath];


// change folderpath from myfolder to yourfolder
NSString *yourFolderPath = [documentPath stringByAppendingPathComponent:@"yourFolder"];
if ([fileManager moveItemAtPath:myFolderPath toPath:yourFolderPath error:NULL]){
    NSLog(@"USER folder success");
}
else {
    NSLog(@"USER folder fail");
}

// attach image2 to self.view
UIImageView *imageView = [[UIImageView alloc] initWithImage:image2];
imageView.center = CGPointMake(100, 200);
[self.view addSubview:imageView];


// it's occurs error.

以下是日志列表。

2012-12-02 17:56:31.273 FolderTest[3372:11303] 成功
2012-12-02 17:56:31.277 FolderTest[3372:11303] USER 文件夹成功
ImageIO: CGImageRead_mapData 'open' failed '/Users/nice7285/Library /Application Support/iPhone Simulator/6.0/Applications/757CBD0E-CB49-4385-AF10-27322FB98381/Documents/myFolder/saveImage@2x.png' error = 2 (No such file or directory)
ImageIO: CGImageRead_mapData 'open' failed '/ Users/nice7285/Library/Application Support/iPhone Simulator/6.0/Applications/757CBD0E-CB49-4385-AF10-27322FB98381/Documents/myFolder/saveImage@2x.png' 错误 = 2(没有这样的文件或目录)

4

2 回答 2

0

我不太确定,但我有一些建议

尝试将创建文件夹行更改为如下所示

[fileManager createDirectoryAtPath:dataPath withIntermediateDirectories:NO attributes:nil error:nil];

使用该函数写入数据

NSError *error;
[imageData writeToFile:savePath options:NSDataWritingAtomic error:&error];

我希望有帮助

于 2012-12-02T11:34:56.897 回答
0

这不是未创建目录的错误,而是由于您将图像保存到沙箱而没有分配而发生此错误

解决方案:-

// write image to saveImage@2x.png file.
NSData *imageData = [[NSData alloc]initWithData:UIImagePNGRepresentation(image1)];

NSString *saveName = @"saveImage@2x.png";
NSString *savePath = [myFolderPath stringByAppendingPathComponent:saveName];
[imageData writeToFile:savePath atomically:YES];

并且还使用分配的对象 ex 获取。

// load from saved the image1 to image2

UIImage *image2 = [[UIImage alloc] initWithContentsOfFile:savePath];
于 2012-12-03T10:41:46.300 回答