如何更改保存图像的文件夹路径?

时间:2012-12-02 09:13:32

标签: iphone objective-c ios ios5 ios6

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

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

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

// 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(没有这样的文件或目录)
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(没有这样的文件或目录)

2 个答案:

答案 0 :(得分:0)

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

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

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

使用此功能写入数据

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

我希望得到帮助

答案 1 :(得分: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];