0

我试图让用户选择删除从相机中挑选的图像,然后保存在 中NSUserDefaults,然后传递到另一个视图。我似乎无法删除图像。它最初会删除图像,但仍将原始拾取的图像放在传递给它的另一个视图上,并在重新加载视图时重新出现。

我需要对我的NSUserDefaults方法进行读取viewDidLoad吗?我尝试了一个if声明,只读出if is中的NSUserDefaults方法,但仍然得到相同的行为viewDidLoadmyPicture!=nil

视图DIdLoad:

 NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
    if ([prefs objectForKey:@"image2"]) {

        NSData *imgData = [prefs objectForKey:@"image2"];
        self.engineersPicture.image = [UIImage imageWithData: imgData];
    }

获取图像并保存到 NSUserDefaults:

 - (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];

}

删除图像:

- (IBAction)deletePicture:(id)sender {

self.myPicture.image = nil;

[[NSUserDefaults standardUserDefaults] removeObjectForKey:@"image2"];
[[NSUserDefaults standardUserDefaults]synchronize ];

}
4

1 回答 1

0

解决它

 - (IBAction)deletePicture:(id)sender {
 self.engineersPicture.image  = [UIImage imageNamed: @"user.png"];

[[NSUserDefaults standardUserDefaults] removeObjectForKey:@"image2"];
[[NSUserDefaults standardUserDefaults]synchronize ];

UIImage *image = [UIImage imageNamed: @"user.png"];
NSData *imageData2 = UIImageJPEGRepresentation(image, 0.5f);
NSString *path2 = [ICUtils pathForDocument:@"engineersPicture.png"];
[imageData2 writeToFile:path2 atomically:NO];
}
于 2013-05-13T18:10:42.723 回答