【问题标题】:How to save photo to directory [duplicate]如何将照片保存到目录[重复]
【发布时间】:2013-08-11 17:02:48
【问题描述】:

我使用

创建了多个目录
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];

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

【问题讨论】:

  • 我在一个旧项目中找到了这一行 [self.library writeImageToSavedPhotosAlbum:img.CGImage metadata:nil completionBlock:nil];但我不是 100% 确定它有效:P
  • @Arbitur 那就是相册.....
  • 哎呀对不起,我把它和别的东西混在一起了:S

标签: ios uiimage directory save


【解决方案1】:

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

【讨论】:

  • 我现在不只是在哪里声明我的目录名称更新答案
  • 我不明白。哪个目录名?我以为您的问题是说目录名称是从选择器中选择的?
猜你喜欢
  • 1970-01-01
  • 2018-11-10
  • 1970-01-01
  • 2018-04-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-09-15
  • 1970-01-01
相关资源
最近更新 更多