2012-12-02 128 views
0

我试图改变保存图像的文件夹路径。 (从我的文件夹到您的文件夹)
我写的代码如下。如何更改保存图片的文件夹路径?

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

如何从已更改的文件夹加载图像文件? 请帮忙。 :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 [email protected] file. 
NSData *imageData = [NSData dataWithData:UIImagePNGRepresentation(image1)]; 

NSString *saveName = @"[email protected]"; 
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月2日17:56:31.273 FolderTest [3372:11303]成功
2012年12月2日17:56:31.277 FolderTest [3372:11303]用户文件夹成功
的ImageIO:CGImageRead_mapData '开放式'失败'/用户/ nice7285 /库/应用程序支持/ iPhone模拟器/6.0/Applications/757CBD0E-CB49-4385-AF10-27322FB98381/Documents/myFolder/[email protected]' 错误= 2(没有这样的文件或目录)
ImageIO:CGImageRead_mapData'open'failed'/ Users/nice7285/Library/Application Support/iPhone Simulator/6.0/Applications/757CBD0E-CB49-4385-AF10-27322FB98381/Documents/myFolder/[email protected]' error = 2(没有这样的文件或目录)

回答

0

真的不知道,但我有一些建议

尝试更改创建文件夹线看起来像这样

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

使用此功能将数据写入

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

我希望帮助

+0

它不能正常工作。不过谢谢你的关注。 –

0

这不是创建目录的错误,但发生此错误是因为您保存了图像E要沙箱不分配

解决方案: -

// write image to [email protected] file. 
NSData *imageData = [[NSData alloc]initWithData:UIImagePNGRepresentation(image1)]; 

NSString *saveName = @"[email protected]"; 
NSString *savePath = [myFolderPath stringByAppendingPathComponent:saveName]; 
[imageData writeToFile:savePath atomically:YES]; 

,并获取使用分配的对象前。

// load from saved the image1 to image2 

UIImage *image2 = [[UIImage alloc] initWithContentsOfFile:savePath]; 
相关问题