2012-10-05 8 views
1

我顯示UIImagePicker從我的UIViewController,在我的UIViewController我有幾個UITextFieldUIImagePicker收到內存警告稱爲viewDidLoad並清除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]; 
    } 
} 
+1

分享您的代碼:) – 2012-10-05 05:38:27

+0

不知道該代碼將如何幫助.. – adit

+0

我們的代碼是不是wrong..but產品UR ActionSheet ..我知道這並不重要,但很多時候它造成問題:)謝謝 – 2012-10-05 05:49:10

回答

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

取消或選擇任何媒體解除後選取器只和發佈。 不要釋放和解僱用videoPicker

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

而且如果已經有picker實例比不再分配一次。

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. 
    } 
} 
+0

我想過也有這個解決方案,但它是那裏最乾淨的解決方案嗎? – adit

+0

是的,這是iOS 6.0之前的唯一解決方案。如前所述,在iOS 6.0中,視圖永遠不會被卸載。只有它的支持商店被卸載。所以這個問題不會再發生在iOS 6.0及更高版本中。 – 2012-10-08 18:19:39