In my app there are multiple images in different folders (5 altogether). How can i put these folders into the document directory?
Then how do I to get the count and the images' name from a particular images folder?
Check Working_with_Directories link.
EDIT : Copy
item
refer this link
Refer NSFileManager for more details:
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);
这是从目录中获取图像的方法
array = [[NSBundle mainBundle] pathsForResourcesOfType:@"png" inDirectory:@"folderName"];
Swift
Path for document directory and store image
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)
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.