Search code examples
iosnsdocumentdirectory

save picture in folder which is located in document direcotory in iphone


i want to create folder in document directory and want to save images in that folder. i have tried but the folder and the images are storing in document directory..But i want to store the images inside the folder.my code is working fine but the images are not saving inside Folder..

NSError *error;
NSString *aDocumentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
NSString *dataPath = [aDocumentsDirectory stringByAppendingPathComponent:@"Folder"];
[[NSFileManager defaultManager] createDirectoryAtPath:dataPath withIntermediateDirectories:NO attributes:nil error:&error];

NSInteger anIndex = 0;

for (UIImage *anImage in _chosenImages) 
{

NSString *anImageName = [NSString stringWithFormat:@"%d.png", anIndex++];
NSString *anImagePath = [NSString stringWithFormat:@"%@/%@", aDocumentsDirectory, anImageName];
NSLog(@"the path is=%@",anImagePath);

NSData *anImageData = UIImagePNGRepresentation(anImage);
[anImageData writeToFile:anImagePath atomically:YES];
}

Solution

  • I guess your "anImagePath" is still pointing to documents directory. edit it as "dataPath" as the argument as below

    NSString *anImagePath = [NSString stringWithFormat:@"%@/%@", dataPath, anImageName];