如何将图像放入文件夹&从文档目录中获取它们

时间:2012-11-06 06:34:39

标签: iphone objective-c nsfilemanager nsbundle nsdocumentdirectory

在我的应用中,不同文件夹中有多个图像(共5个)。如何将这些文件夹放入文档目录?

那么如何从特定的图像文件夹中获取计数和图像的名称?

4 个答案:

答案 0 :(得分:1)

检查Working_with_Directories链接。

编辑Copy item参考this链接

有关详细信息,请参阅NSFileManager

答案 1 :(得分:0)

UIImage *dimage = [UIImage imagedNamed:@"yourImage.png"]; //get the image name here
 NSString *docDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];

 NSString *imageName = @"myImageName.png";
 NSArray *patharr=[ImagePath componentsSeparatedByString:@"."];
 NSString* Ext=[NSString stringWithFormat: @"%@",[patharr objectAtIndex:1]];
 NSString *FilePath = [NSString stringWithFormat: [NSString stringWithFormat:@"%@/%@",docDir,ImagePath]];


 NSData *data = [NSData dataWithData:UIImagePNGRepresentation(dimage)];
 [data writeToFile:FilePath atomically:YES];
  

进一步获取文档目录的文件名的内容......

 NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsPath = [paths objectAtIndex:0]; 
    NSArray *dirContents = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:documentsPath error:nil];
    NSMutableArray *array=[[NSMutableArray alloc]init];
    for (NSString *dir in dirContents) {
        if ([dir hasSuffix:@".png"]) {
            NSRange range = [dir rangeOfString:@"."];
            NSString *name = [dir substringToIndex:range.location];
            if (![name isEqualToString:@""]) {

                [array addObject:name];
            }
        }
    } 
NSLog(@"document folder content list %@ ",array);

答案 2 :(得分:0)

这是您从目录中获取图像的方法

array = [[NSBundle mainBundle] pathsForResourcesOfType:@"png" inDirectory:@"folderName"];

答案 3 :(得分:0)

<强>夫特
文档目录和商店图像的路径

func getDocumentsDirectory() -> URL {
    let paths = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)
    let documentsDirectory = paths[0]
    return documentsDirectory
}

if let image = UIImage(named: "example.png") {
    if let data = UIImagePNGRepresentation() {
        let filename = getDocumentsDirectory().appendingPathComponent("copy.png")
        try? data.write(to: filename)
    }
}