Hi all,
Yup i know its very basic for many but for some it may be useful ;)
//Get Array of paths :
Yup i know its very basic for many but for some it may be useful ;)
//Get Array of paths :
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
//Get Documents Folder
NSString *documentsDirectory = [paths objectAtIndex:0];
//Create path of File, may be inside any directory
NSString *dataPath = [NSString stringWithFormat:@"%@/%@",documentsDirectory,currentElement.iD];
if (![[NSFileManager defaultManager] fileExistsAtPath:dataPath])
{
//For Creating directory
[[NSFileManager defaultManager] createDirectoryAtPath:dataPath
withIntermediateDirectories:NO
attributes:nil
error:nil];
}
[self saveImage:image withFileName:currentElement.iD ofType:@"png" inDirectory:dataPath];
//Here is the called method definition
- (void) saveImage:(UIImage *)image
withFileName:(NSString *)imageName
ofType:(NSString *)extension
inDirectory:(NSString *)directoryPath
{
if ([[extension lowercaseString] isEqualToString:@"png"])
{
[UIImagePNGRepresentation(image) writeToFile:[directoryPath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.%@", imageName, @"png"]]
options:NSAtomicWrite
error:nil];
}
else if ([[extension lowercaseString] isEqualToString:@"jpg"] ||
[[extension lowercaseString] isEqualToString:@"jpeg"])
{
[UIImageJPEGRepresentation(image, 1.0) writeToFile:[directoryPath stringByAppendingPathComponent: [NSString stringWithFormat:@"%@.%@", imageName, @"jpg"]]
options:NSAtomicWrite
error:nil];
}
else
{
NSLog(@"Image Save Failed\nExtension: (%@) is not recognized, use (PNG/JPG)", extension);
}
}
Hope it helps
Enjoy :)
No comments:
Post a Comment