I wanted to save an image that has been taken to the application documents directory, but for some reason, the counter always going up but there are no picture inside the directory. what seems to be the problem?
Thanks alot!
- (void)imagePickerController:(UIImagePickerController*)picker didFinishPickingMediaWithInfo:(NSDictionary*)info
{
[picker dismissModalViewControllerAnimated:YES];
self.imageViewRecipt.image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
//
// Getting the current counter value
//
NSUserDefaults * prefs = [NSUserDefaults standardUserDefaults];
int imageCounter;
imageCounter = [[prefs objectForKey:@"imageCounter"]intValue];
//
// Obtaining saving path
//
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *imagePath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"image%i.png",imageCounter]];
//
// Extracting image from the picker and saving it
//
NSString *mediaType = [info objectForKey:UIImagePickerControllerMediaType];
if ([mediaType isEqualToString:@"public.image"])
{
UIImage *editedImage = [info objectForKey:UIImagePickerControllerEditedImage];
NSData *webData = UIImagePNGRepresentation(editedImage);
[webData writeToFile:imagePath atomically:YES];
//
// Saving the new counter into the plist
//
imageCounter++;
[prefs setInteger:imageCounter forKey:@"imageCounter"];
[prefs synchronize];
}
}
OK, so I fixed it by removing the “mediaType” and the if statment and replace it with this:
and now it’s working just fine.