我正在尝试更改保存图像的文件夹路径。(从 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(没有这样的文件或目录)