1

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

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

4

4 回答 4

1

检查Working_with_Directories链接。

编辑Copy item参考这个链接

有关详细信息,请参阅NSFileManager

于 2012-11-06T06:52:31.437 回答
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);
于 2012-11-06T06:53:01.453 回答
0

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

array = [[NSBundle mainBundle] pathsForResourcesOfType:@"png" inDirectory:@"folderName"];
于 2012-11-06T06:53:10.043 回答
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)
    }
}
于 2017-02-25T02:25:43.040 回答