在我的应用程序中,不同文件夹中有多个图像(总共 5 个)。如何将这些文件夹放入文档目录?
那么如何从特定图像文件夹中获取计数和图像名称?
在我的应用程序中,不同文件夹中有多个图像(总共 5 个)。如何将这些文件夹放入文档目录?
那么如何从特定图像文件夹中获取计数和图像名称?
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);
这是您从目录中获取图像的方式
array = [[NSBundle mainBundle] pathsForResourcesOfType:@"png" inDirectory:@"folderName"];
文档目录和存储图像的快速路径
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)
}
}