Reputation: 2466
I have a picker that saves my image in NSDocumentDirectory
, here:
for (int i = 0; i < image.count; i++) {
NSLog(@"%@", [image objectAtIndex:i]);
NSArray *paths = NSSearchPathForDirectoriesInDomains( NSDocumentDirectory, NSUserDomainMask ,YES );
NSString *documentsDirec = [paths objectAtIndex:0];
NSString *savedPath = [documentsDirec stringByAppendingPathComponent:[NSString stringWithFormat:@"myImages%d.png", i]];
ALAssetRepresentation *rep = [[image objectAtIndex: i] defaultRepresentation];
UIImage *image = [UIImage imageWithCGImage:[rep fullResolutionImage]];
NSData *imageData = UIImagePNGRepresentation(image);
[imageData writeToFile:savedPath atomically:YES];
NSLog(@"saving at:%@",savedPath);
}
How to determine or implement to check if an existing image/imageName is already there. Then if an images exist it will not add the same image/imageName even if its still picked in the picker.
Upvotes: 2
Views: 1978
Reputation: 3667
Use the below code for checking a specific image is already exists in the documents directory:
NSString *documentsDirectoryPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *imageName = [documentsPath stringByAppendingPathComponent:@"imageName.jpg"];
BOOL fileExists = [[NSFileManager defaultManager] imageName];
In your code you can add it like this:
for (int i = 0; i < image.count; i++) {
NSLog(@"%@", [image objectAtIndex:i]);
NSArray *paths = NSSearchPathForDirectoriesInDomains( NSDocumentDirectory, NSUserDomainMask ,YES );
NSString *documentsDirec = [paths objectAtIndex:0];
NSString *savedPath = [documentsDirec stringByAppendingPathComponent: [NSString stringWithFormat:@"myImages%d.png", i]];
BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:savedPath];
//Save only if file not exists
if(!fileExists) {
ALAssetRepresentation *rep = [[image objectAtIndex: i] defaultRepresentation];
UIImage *image = [UIImage imageWithCGImage:[rep fullResolutionImage]];
NSData *imageData = UIImagePNGRepresentation(image);
[imageData writeToFile:savedPath atomically:YES];
NSLog(@"saving at:%@",savedPath);
}
}
Upvotes: 4
Reputation: 38239
Do this:
for (int i = 0; i < image.count; i++)
{
NSLog(@"%@", [image objectAtIndex:i]);
NSArray *paths = NSSearchPathForDirectoriesInDomains( NSDocumentDirectory, NSUserDomainMask ,YES );
NSString *documentsDirec = [paths objectAtIndex:0];
NSString *savedPath = [documentsDirec stringByAppendingPathComponent:[NSString stringWithFormat:@"myImages%d.png", i]];
if(![[NSFileManager defaultManager]fileExistsAtPath:savedPath]) //if not exits then save image
{
ALAssetRepresentation *rep = [[image objectAtIndex: i] defaultRepresentation];
UIImage *image = [UIImage imageWithCGImage:[rep fullResolutionImage]];
NSData *imageData = UIImagePNGRepresentation(image);
[imageData writeToFile:savedPath atomically:YES];
NSLog(@"saving at:%@",savedPath);
}
}
Upvotes: 0
Reputation: 6718
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"imageName.jpg"];
if ([[NSFileManager defaultManager] fileExistsAtPath:filePath]) {
NSLog(@"file exists at the path");
}
else
NSLog(@"file doesnt exist");
I think it will be helpful to you.
Upvotes: 1