0

私はこれについて多くのことを研究しました。ボタンに触れると、デバイスから画像を選択して画面に表示する必要があります。

しかし、ボタンがタッチ/押されると、アプリがクラッシュします。次のコード行からクラッシュします。

[self presentModalViewController:myPhotopicker animated:YES];

Xcode 4.2 を使用して iPad アプリを開発しています。テストには iPad 5.0 シミュレーターを使用しています。私のシステムは Mac OS X バージョン 10.6.8 で動作します。

ボタンが押されると、次の関数が呼び出されます。

-(IBAction)getPhoto:(id)sender
{
    if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {
        if (myPhotopicker==nil) { myPhotopicker = [[UIImagePickerController alloc] init];
            myPhotopicker.delegate = self; }// create once!
        myPhotopicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;

        [self presentModalViewController:myPhotopicker animated:YES];

    } else {
        NSString *str = @"Photo Album is not available!";     

    }   

}
4

1 に答える 1

0

コードを試してみたところ、シミュレーターでクラッシュを再現できました。しかし、iOS 4.2 を搭載した iPhone 4 では問題なく動作しています。

そうは言っても、シミュレーターのギャラリーにいくつかの写真を含めました。(シミュレーター内で Safari を起動し、いくつかのページを開き、写真を長押ししてメニューから [保存] を選択して保存します。)

今、シミュレーターは書き込みます

2012-05-08 15:53:55.605 test[5870:f803] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'On iPad, UIImagePickerController must be presented via UIPopoverController'

コンソールに。

さて、読んで、完了:

-(IBAction)getPhoto:(UIButton *)sender
{
    if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) 
    {
        if (myPhotopicker==nil) {
            myPhotopicker = [[UIImagePickerController alloc] init];
            myPhotopicker.delegate = self;
        }// create once!
        myPhotopicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;

        if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
            // iPad Code:
            UIPopoverController *popover =
                [[UIPopoverController alloc] initWithContentViewController:myPhotopicker];
            [popover presentPopoverFromRect:sender.bounds
                                     inView:self.view
                   permittedArrowDirections:UIPopoverArrowDirectionAny
                                   animated:YES];
        } else {
            // iPhone Code:
            [self presentModalViewController:myPhotopicker animated:YES];
        }
    } else {
        NSLog(@"Photo Album is not available!");
    }     
}

そして今、それは次のようになります。
ここに画像の説明を入力

于 2012-05-08T14:04:40.560 に答える