如何将照片保存到目录

时间:2013-08-11 17:02:48

标签: ios uiimage directory save

我使用

创建了多个目录
NSArray *directoryNames = [NSArray arrayWithObjects:@"t",@"b",@"p",@"c", @"S", @"h", @"o",nil];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0]; // Get documents folder

for (int i = 0; i < [directoryNames count] ; i++) {
    NSString *dataPath = [documentsDirectory stringByAppendingPathComponent:[directoryNames objectAtIndex:i]];
    if (![[NSFileManager defaultManager] fileExistsAtPath:dataPath])
        [[NSFileManager defaultManager] createDirectoryAtPath:dataPath withIntermediateDirectories:NO attributes:nil error:nil]; 

但是我想对UIImage上显示的照片进行排序,并使用pickerview对用户选择的照片进行排序。我想知道保存到我做的这些特定目录的代码。任何帮助是极大的赞赏!

更新我知道

在您的应用中的任何位置使用您创建的目录。

NSArray *directoryNames = [NSArray arrayWithObjects:@"tops",@"bottoms",@"right",@"left",nil];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0]; // Get documents folder

// This will return the folder name in the 0 position. In yours "tops"
NSString *topDirPath = [documentsDirectory stringByAppendingPathComponent:[directoryNames objectAtIndex:0]];
// This will return the file path in your "tops" folder
NSString *filePath = [topDirPath stringByAppendingPathComponent:@"MYIMAGE"];

存储图像文件

NSData *imageDataToStore = UIImagePNGRepresentation(image);
[imageDataToStore writeToFile:filePath atomically:YES];

我不知道在哪里声明我的目录名

1 个答案:

答案 0 :(得分:1)

要将图像保存到文件夹中的文件,您需要获取图像的数据(UIImageJPEGRepresentation),然后将该数据保存到磁盘(writeToFile:atomically:)。

相关问题