2015-04-30 31 views
0

我是iOS編程的新手,因此我提前致歉。我知道這個話題已被多次報道(我搜索了),但我似乎無法解決我的問題,這就是我發佈的原因。iOS應用程序 - 試圖從圖庫中獲取iOS照片但出現錯誤

我試圖訪問iOS的照片庫,但我不斷收到兩個錯誤:

一個是 「應用程序試圖把目標一零模態視圖控制器。」

編輯:上述錯誤是通過在ChooseExsisting中啓動_picker來解決的,正如評論中所建議的那樣。

其他

[CameraController ChooseExsiting:]:無法識別的選擇發送到實例0x157e11330'

我的代碼如下:

- (IBAction)ChooseExsiting { 
UIImagePickerController *pickerController = [[UIImagePickerController alloc] 
              init]; 
pickerController.delegate = self; 
[self.picker setSourceType:UIImagePickerControllerSourceTypePhotoLibrary]; 
[self presentViewController:_picker animated:YES completion:NULL]; } 

我想象我ChooseExsisting代碼不正確。會有人有任何建議嗎?我將不勝感激。

+0

請告訴你你在iOS模擬器或設備上運行。 –

+0

你的模擬器中沒有照片...照片庫,所以它給這個錯誤..根據我的意見。 –

+0

在我的設備上運行,其中有許多照片。 在更改第一個回覆建議更改的內容後(init選擇器),我收到此錯誤: – calrol2009

回答

1

在你的ChooseExisting方法中,你將一個控制器實例化成一個局部變量,但是然後用_picker屬性變量來調用它,這可能是零。可以從局部變量中呈現控制器,或者像TakePhoto方法那樣初始化屬性。

編輯:至於第二部分,如果您將它們連接到故事板中的Tap處理程序,則兩個IBActions都有錯誤的方法簽名。他們應該是這樣的:

- (IBAction) TakePhoto:(id)sender

- (IBAction) ChooseExsiting:(id)sender

+0

謝謝!但是,我遇到了另一個錯誤... [CameraController ChooseExsiting:]:無法識別的選擇器發送到實例0x157e11330' 我做了一個搜索,但答案很模糊。任何想法? – calrol2009

+0

我認爲你將該方法綁定到Storyboard中的tap處理程序。在這種情況下,你的方法必須有這樣的簽名:ChooseExsiting:(id)sender - 你缺少參數 – feb

+0

我已經編輯了我的答案了一些更多的細節。 – feb

0

更改您的ChooseExisitng方法如下之一:

- (IBAction)ChooseExsiting:(id) sender { 
    UIImagePickerController *pickerController = [[UIImagePickerController alloc] 
             init]; 
    pickerController.delegate = self; 
    [pickerController setSourceType:UIImagePickerControllerSourceTypePhotoLibrary]; 
    [self presentViewController:pickerController animated:YES completion:NULL]; } 

- (IBAction)ChooseExsiting:(id) sender { 
    self.picker = [[UIImagePickerController alloc] 
             init]; 
    self.picker.delegate = self; 
    [self.picker setSourceType:UIImagePickerControllerSourceTypePhotoLibrary]; 
    [self presentViewController:self.picker animated:YES completion:NULL]; } 
+0

不幸的是,這兩個崩潰與錯誤:'NSInvalidArgumentException',原因:' - [CameraController ChooseExsiting:]:無法識別的選擇發送到實例0x13fe0fca0' – calrol2009

+0

更新我的代碼。你如何創建ChooseExisiting方法?你可以發佈一些代碼。您的TakePhoto方法是否正常工作? –

0
You can try this . It work for me 
Follow below three steps1.First of all you need to add below delegate in .h file (UIImagePickerControllerDelegate,UIPickerViewDelegate) 
2.Add below three methods in your controller 
3. On button click call below methods 
here choose photo for open gallary. so on button click call choose photo method 
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info 
{ 
    CGSize newSize=CGSizeMake(150, 150); 
    UIGraphicsBeginImageContextWithOptions(newSize, NO, 0.0); 
    [[info objectForKey:UIImagePickerControllerEditedImage] drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)]; 
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 
    img_profile_pic.image = newImage; 
    [self dismissViewControllerAnimated:YES completion:nil]; 
    isProfilePic=TRUE; 
} 
- (void)showImagePickerForSourceType:(UIImagePickerControllerSourceType)sourceType 
{ 
    UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init]; 
    imagePickerController.allowsEditing = YES; 
    imagePickerController.modalPresentationStyle = UIModalPresentationCurrentContext; 
    imagePickerController.sourceType = sourceType; 
    imagePickerController.delegate = self; 
    imagePickerController = imagePickerController; 
    [self presentViewController:imagePickerController animated:YES completion:nil]; 
} 

    - (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker 
    { 
     [self dismissViewControllerAnimated:YES completion:nil]; 
    } 


    -(void) takePhoto{ 
     [self showImagePickerForSourceType:UIImagePickerControllerSourceTypeCamera]; 
    } 
    -(void) choosePhoto{ 
    [self showImagePickerForSourceType:UIImagePickerControllerSourceTypePhotoLibrary]; 
    } 
0

而不是做



    if ([UIImagePickerController isSourceTypeAvailable:!UIImagePickerControllerSourceTypeCamera]) { 

    self.picker = [[UIImagePickerController alloc] init]; 
    self.picker.delegate = self; 
    [self.picker setSourceType:UIImagePickerControllerSourceTypeCamera]; 
    [self presentViewController:_picker animated:YES completion:NULL]; 
    } } 

的你應該這樣做



    if ([UIImagePickerController isSourceTypeAvailable:!UIImagePickerControllerSourceTypeCamera]) { 
    if(!_picker){ 
    _picker = [[UIImagePickerController alloc] init]; 
    _picker.delegate = self; 
    [_picker setSourceType:UIImagePickerControllerSourceTypeCamera]; 
    [self presentViewController:_picker animated:YES completion:NULL]; 

    }else{ 

    [self presentViewController:_picker animated:YES completion:NULL]; 

    }}} 

你應該呈現前初始化_picker屬性,現在你要初始化VC的本地屬性,或者您也可以現在的自己.picker代替你的代碼中的_picker。