UIImagePicker收到名为viewDidLoad的内存警告并清除UITextField

时间:2012-10-05 05:36:25

标签: iphone objective-c ios ipad

我在我的UIImagePicker UIViewController中显示UIViewControllerUITextField我有一些UITextField

现在,用户已在此UIImagePicker中填写了一些值,然后他从UITextField中选择了一个图像,之后它收到了内存警告,并再次调用viewDidLoad,但随后又调用了所有数据用户在- (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]; } } 中输入的内容丢失了。我该如何处理?

代码:

{{1}}

2 个答案:

答案 0 :(得分:1)

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

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

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

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

因此,在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.
    }
}

答案 1 :(得分: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];
}

如果已经有选择器实例,那么不要再重新分配。