1

我正在展示UIImagePicker我的一个UIViewController,在我的UIViewController我有几个UITextField

现在用户已经在其中填写了一些值,UITextField然后他从 中选择了一张图像UIImagePicker,之后它收到了内存警告,并再次调用 viewDidLoad,但是用户在 中输入的所有数据UITextField都丢失了。我该如何处理?

代码:

- (void) showImageSelector: (UITapGestureRecognizer *) tapGestureRecognizer
{
    UIActionSheet *actionSheet;
    if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
        actionSheet = [[UIActionSheet alloc] initWithTitle:[PNRConstants kChoosePhoto] delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:nil otherButtonTitles:[PNRConstants kPhotoLibrary], [PNRConstants kTakeAPicture], nil];
    }
    else{
        actionSheet = [[UIActionSheet alloc] initWithTitle:[PNRConstants kChoosePhoto] delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:nil otherButtonTitles:[PNRConstants kPhotoLibrary], nil];
    }
    [actionSheet showInView:self.view.window];
}

#pragma -
#pragma UIActionSheetDelegate
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
    if (buttonIndex != actionSheet.cancelButtonIndex) {
        UIImagePickerController *controller = [[UIImagePickerController alloc] init];
        controller.delegate = self;
        controller.allowsEditing = YES;
        if (buttonIndex == 0) {
            // Photo Library
            controller.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
        }
        else {
            // Camera
            controller.sourceType = UIImagePickerControllerSourceTypeCamera;
        }
        [self presentViewController:controller animated:YES completion:nil];
    }
}
4

2 回答 2

1

当内存不足且不需要视图控制器的视图时,视图将从内存中卸载。(然而,iOS 6.0 中的情况并非如此,因为视图不再从内存中清除)。

因此,当您收到内存警告时,视图将从内存中卸载。当你回到它时,视图控制器的视图被再次请求,因此它被再次创建/加载。因此,您viewDidLoad再次被调用。

此时,这是加载的全新视图。它已经失去了它以前的所有属性。

为了避免这种情况,您可以做的是 - 当用户设置了文本字段的属性(很可能是文本)时,您必须将这些值保存在变量等的某个位置。

因此,在您的viewDidLoad中,检查该变量是否存在和/或是否包含任何值。如果没有,则表示这是第一次加载视图。在这里,你什么都不做。

但是,一旦用户在文本字段中设置了一些文本,控制器的视图就会被卸载,稍后它会再次加载,它viewDidLoad会被调用。在这里,此时变量保存了之前输入的文本值。所以设置textField的集合。

简而言之,代码看起来像这样:

-(void) viewDidLoad {
    [super viewDidLoad];

    if (yourVariableHoldingTextInput) {
        // Variable holding the text field's input exists. This means user had set some input to the text field. View was purged from memory and is now loaded again.
        yourTextField.text = yourVariableHoldingTextInput; // Essentially, set the text field's text with the variable's value.
    }else {
        // Here we do nothing. This clearly means the controller's view loaded very first time.
    }
}
于 2012-10-05T06:00:08.927 回答
0

UIImagePicker 从处理器消耗更多内存。所以你需要先解决这个内存警告。

当我将相机和照片库用于视频时,我遇到了同样的问题。

在我使用技巧并解决之后。这里有技巧

全局定义 UIImagePickerController 和访问

在这里我使用代码来调用库

-(UIImagePickerController *)getImagePickerController{
    if (!imagePickerController) {
        imagePickerController = [[UIImagePickerController alloc]init];
    }
    return imagePickerController;
}

将此 UIImagePicker 实例调用到 videoPicker 后

videoPicker = [self getImagePickerController]; 
    videoPicker = [[UIImagePickerController alloc] init];
    videoPicker.sourceType =  UIImagePickerControllerSourceTypePhotoLibrary;
    videoPicker.delegate = self;
    videoPicker.allowsEditing = YES;
    videoPicker.mediaTypes =  [NSArray arrayWithObject:(NSString *)kUTTypeMovie];    
    videoPicker.videoQuality = UIImagePickerControllerQualityTypeHigh;
    videoPicker.videoMaximumDuration = 30.0;
    NSArray *sourceTypes = [UIImagePickerController availableMediaTypesForSourceType:videoPicker.sourceType];
    if (![sourceTypes containsObject:(NSString *)kUTTypeMovie ])
    {
        NSLog(@"no video");
    }
    else
    {
        [self presentModalViewController:videoPicker animated:YES];
    }

取消或选择任何媒体后,仅关闭Picker并释放。不要使用videoPicker释放和关闭。

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{   
    [picker dismissModalViewControllerAnimated:YES];
}

如果已经存在选择器实例,则不要再次分配。

于 2012-10-05T05:55:01.030 回答