0

我已经创建了多个这样的目录

NSArray *directoryNames = [NSArray arrayWithObjects:@"hats",@"bottoms",@"right",@"left",nil];

但是我不知道如何保存到特定目录中。我这样保存

NSString *filePath = [folderPath stringByAppendingPathComponent:@"IMAGE_NAME_HERE.PNG"]; 
NSData *imageData = UIImagePNGRepresentation(captureImage.image);
[imageData writeToFile:filePath atomically:YES];

但是我不知道如何指定目录。我只希望用户保存到目录帽子中。我怎样才能做到这一点?

4

1 回答 1

0
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
NSString *docsPath = [paths objectAtIndex:0]; 
NSString *imageCacheDirPath = [docsPath stringByAppendingPathComponent:@"hats"];
NSString *imageCachePath = [imageCacheDirPath stringByAppendingPathComponent:@"IMAGE_NAME_HERE.PNG"]; 

if (![[NSFileManager defaultManager] fileExistsAtPath: imageCacheDirPath])
      [[NSFileManager defaultManager] createDirectoryAtPath:imageCacheDirPath 
                                withIntermediateDirectories:NO 
                                                 attributes:nil
                                                      error:NULL];

[[NSFileManager defaultManager] createFileAtPath:imageCachePath 
                                        contents:dataObj 
                                      attributes:nil];
于 2013-08-15T12:06:04.330 回答