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

时间:2021-12-18 06:11:54

In my app there are multiple images in different folders (5 altogether). How can i put these folders into the document directory?

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

Then how do I to get the count and the images' name from a particular images folder?

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

4 个解决方案

#1


1  

Check Working_with_Directories link.

检查Working_with_Directories链接。

EDIT : Copy item refer this link

编辑:复制项目请参阅此链接

Refer NSFileManager for more details:

有关更多详细信息,请参阅NSFileManager:

#2


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];

Further getting the contents of documents directory's filenames...

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

 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);

#3


0  

This is how you can get the images from your directory

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

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

#4


0  

Swift
Path for document directory and store image

文档目录和商店图像的Swift路径

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)
    }
}

#1


1  

Check Working_with_Directories link.

检查Working_with_Directories链接。

EDIT : Copy item refer this link

编辑:复制项目请参阅此链接

Refer NSFileManager for more details:

有关更多详细信息,请参阅NSFileManager:

#2


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];

Further getting the contents of documents directory's filenames...

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

 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);

#3


0  

This is how you can get the images from your directory

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

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

#4


0  

Swift
Path for document directory and store image

文档目录和商店图像的Swift路径

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)
    }
}