您需要写入实际在iPhone上的目录。大多数情况下,您需要写入文档目录。你可以得到它的路径是这样的:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
这给你基本的文件目录,如果你要的文件夹放在你需要你去做这样之前创建该文件夹的子目录。
NSString *folderPath = [documentsDirectory stringByAddingPathComponent:@"myFolder/images"];
NSError *error = nil;
NSFileManager *fm = [[NSFileManager alloc] init]
[fm createDirectoryAtPath:folderPath withIntermediateDirectories:YES attributes:nil error:&error];
然后到你的图像数据保存到该文件夹,你会做这样的事情
NSString *imageDataPath = [folderPath stringByAppendingPathComponent:@"myImage"];
BOOL success = [imageData writeToFile:imageDataPath];
此外,您还可以将的NSLog它imageDataPath会给你该文件被保存,以便确切位置如果您使用的是iOS模拟器,则可以导航到finder中的实际文件。
你认为你的iPhone有这样的目录结构(提示:事实并非如此)?你检查了对'writeToFile:'调用的返回值吗?您需要在应用的沙箱中写入有效的路径。做一些搜索。这已经在这里多次报道。 – rmaddy 2014-12-03 23:41:02
模拟器实际上应该将图像保存到桌面。 – Andy 2014-12-04 02:00:20