Note that there are some explanatory texts on larger screens.

plurals
  1. PODelete image thats stored in NSSUserDefaults
    text
    copied!<p>Im trying to give user option to delete an image picked from camera that is then saved in <code>NSUserDefaults</code>, then passed to another view. I cant seem to keep the image deleted. It deletes the image initially but still puts the original picked image on the other view its passed to and re appears on reload of the view. </p> <p>Do I need to do something with the reading out of my <code>NSUserDefaults</code> method in <code>viewDidLoad</code>? I tried an <code>if</code> statement to only read out the <code>NSUserDefaults</code> method in <code>viewDidLoad</code> if <code>myPicture</code> was <code>!=nil</code> but still got the same behaviour </p> <p>viewDIdLoad:</p> <pre><code> NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults]; if ([prefs objectForKey:@"image2"]) { NSData *imgData = [prefs objectForKey:@"image2"]; self.engineersPicture.image = [UIImage imageWithData: imgData]; } </code></pre> <p>Get image and save to NSUserDefaults:</p> <pre><code> - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info { DebugLog(@"info dict: %@", info); [picker dismissModalViewControllerAnimated:YES]; UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage]; self.engineersPicture.backgroundColor = [UIColor whiteColor]; self.engineersPicture.image = image; NSData *imageData2 = UIImageJPEGRepresentation(image, 0.5f); NSString *path2 = [ICUtils pathForDocument:@"engineersPicture.png"]; [imageData2 writeToFile:path2 atomically:NO]; NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults]; [prefs setObject:imageData2 forKey:@"image2"]; [prefs synchronize]; self.engineersPicture.image = [UIImage imageWithData:imageData2]; [picker dismissModalViewControllerAnimated:YES]; } </code></pre> <p>Delete Image:</p> <pre><code>- (IBAction)deletePicture:(id)sender { self.myPicture.image = nil; [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"image2"]; [[NSUserDefaults standardUserDefaults]synchronize ]; } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload