34127

Check existing image in NSDocumentDirectory

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.

Answer1:

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

Answer2:

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.

Answer3:

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

Recommend

  • How to parse XML file from a server?
  • How do i remove a substring from an nsstring?
  • Detecting Current Item in AVQueuePlayer and Knowing when it Changes?
  • NSImage transparency
  • RestKit POST response with wrong mapping
  • How to set background color in CCLabelTTF in cocos2d
  • Perform Segue after clicking on search result
  • SetText is Deprecated
  • iOS - Issue with displaying battery status [duplicate]
  • Adding a UILabel's layer to another layer (Separate UIView)
  • BAD ACCESS after block within a block is called (iOS)
  • Zoom in Scroll view with UIImage
  • Authentication Error while extracting user information with Facebook Graph API
  • How to hit post request with NSDictionary as parameter
  • Using JSON Framework on iPhone - HELP!
  • autofill username and password in an HTML page opening in UIWebView
  • How to output JSon data in Objective-C
  • way to start activity indicator on click event of button before its touch ends?
  • Yelp API ios getting a larger image
  • Change UISearchBar background image in iOS11
  • Check existing image in NSDocumentDirectory
  • How to make Images in UITableViewCell scroll smoothly?
  • Badge value on “More” tab
  • CMCalibratedMagneticField doesn't update
  • NSURL Always Returns Nil
  • How to add callout into individual annotation in map view
  • How do you close open files using Swift?
  • Display Local HTML file from documents directory in a UIWebView on iPhone
  • moving an object without actually touching the object
  • gap in the plot area for geom_area
  • Webdriver kicks the bucket on “find_element”
  • adding zeros in objective-c string formats
  • Concise R data.table syntax for modal value (most frequent) by group
  • How to remove duplicate buttons
  • UIimage to char* conversion
  • Unexpected Error Google Apps Script Fetch
  • R Impute NA's by Linear Increase Depending on Time Interval
  • How to customize whisker lines on a geom_box plot differently than the lines of the box itself
  • Is mp4 stream able with ffserver?
  • JSON encode and decode on PHP