Nasir
Nasir

Reputation: 431

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];
}

Upvotes: 0

Views: 856

Answers (2)

Suryakant Sharma
Suryakant Sharma

Reputation: 3960

try this..

[[NSFileManager defaultManager] createDirectoryAtPath:dataPath withIntermediateDirectories:YES attributes:nil error:&error];

actually you arr passing NO in "withIntermediateDirectories" parameter, and if you pass NO to this than if any of the intermediate parent directories does not exist. This method also fails if any of the intermediate path elements corresponds to a file and not a directory.

Upvotes: 0

Saify
Saify

Reputation: 190

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];

Upvotes: 2

Related Questions