Reputation: 1950
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?
Upvotes: 1
Views: 2632
Reputation: 79636
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)
}
}
Upvotes: 0
Reputation: 38239
Check Working_with_Directories link.
EDIT : Copy
item
refer this link
Refer NSFileManager for more details:
Upvotes: 1
Reputation: 2163
This is how you can get the images from your directory
array = [[NSBundle mainBundle] pathsForResourcesOfType:@"png" inDirectory:@"folderName"];
Upvotes: 0
Reputation: 4249
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);
Upvotes: 0