在我的应用中,不同文件夹中有多个图像(共5个)。如何将这些文件夹放入文档目录?
那么如何从特定的图像文件夹中获取计数和图像的名称?
答案 0 :(得分:1)
答案 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)
}
}