IOS 在Ipad 橫屏 上使用UIImagePickerController


轉載前請注明來源:http://www.cnblogs.com/niit-soft-518/p/4381328.html

最近在寫一個ipad的項目,該項目必須是橫屏。進入正題,有一項功能是要調用系統的牌照和相冊,於是本屌用了 UIImagePickerController,興沖沖的運行了一下,呵呵,崩掉了,然后帶着高逼格去查蘋果文檔,一堆英文,不懂,好吧,百度一下,發現好多同道中人說 UIImagePickerController必須要豎屏,不能橫屏~這讓我差點淚崩,又繼續看蘋果文檔,發現了新大陸:

 

  1. Present the user interface. On iPhone or iPod touch, do this modally (full-screen) by calling the presentViewController:animated:completion: method of the currently active view controller, passing your configured image picker controller as the new view controller.

    On iPad, the correct way to present an image picker depends on its source type, as summarized in this table:

     

    Camera

    Photo Library

    Saved Photos Album

    Use full screen

    Must use a popover

    Must use a popover

    The table indicates that on iPad, if you specify a source type of  UIImagePickerControllerSourceTypePhotoLibrary or UIImagePickerControllerSourceTypeSavedPhotosAlbum, you must present the image picker using a popover controller, as described in Presenting and Dismissing the Popover. If you attempt to present an image picker modally (full-screen) for choosing among saved pictures and movies, the system raises an exception.

    On iPad, if you specify a source type of UIImagePickerControllerSourceTypeCamera, you can present the image picker modally (full-screen) or by using a popover. However, Apple recommends that you present the camera interface only full-screen.

 

好吧,以我四級未過的英語水平就不做翻譯了,大概的意思就是說 如果在ipad中用uiimagepickercontroller,如果調用本地相冊,必須使用POPOverController,如果要調用本地相機,可是將modalPresentationStyle屬性設置為UIModalPresentationFullScreen也可以設置為popover,蘋果推薦設置為全屏的fullscreen。設置然后我就試着寫了一下,果然很OK!

以下是代碼:

  一個調用相冊的功能:

/*
 相冊
 */
-(void)Photo:(UIButton *)sender{
    imagePicker = [[UIImagePickerController alloc] init];
    imagePicker.delegate = self;
    imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    // 編輯模式
    imagePicker.allowsEditing=NO;
    UIPopoverController *pop=[[UIPopoverController alloc]initWithContentViewController:imagePicker];
//    [self presentViewController:imagePicker animated:YES completion:NULL];
    [pop presentPopoverFromRect:sender.bounds inView:sender permittedArrowDirections:UIPopoverArrowDirectionLeft animated:YES];
}

 

效果:

 

 

 

iOS8以下推薦使用

 

    UIAlertAction *action_one = [UIAlertAction actionWithTitle:@"相冊" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {
            NSLog(@"打開相冊");
            [imgPicker setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
            imgPicker.modalPresentationStyle = UIModalPresentationPopover; //調用本地相冊,必須使用popover
            popPC =imgPicker.popoverPresentationController;
            [popPC setBarButtonItem:[[UIBarButtonItem alloc]initWithCustomView:bt]];
            popPC.permittedArrowDirections = UIPopoverArrowDirectionAny;
            popPC.delegate=self;
            [self presentViewController:imgPicker animated:false completion:nil];
        }else{
            [SVProgressHUD showErrorWithStatus:@"請前往設置界面允許程序訪問相冊!"];
        }
    }];
    [sheet addAction:action_one];
    UIAlertAction *action_two = [UIAlertAction actionWithTitle:@"相機" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
            NSLog(@"打開相機");
            [imgPicker setSourceType:UIImagePickerControllerSourceTypeCamera];
            imgPicker.modalPresentationStyle = UIModalPresentationFullScreen;  //蘋果推薦使用全屏
            popPC =imgPicker.popoverPresentationController;
            [popPC setBarButtonItem:[[UIBarButtonItem alloc]initWithCustomView:bt]];
            [popPC setSourceRect:CGRectMake(0, 0, 600, 600)];
            popPC.permittedArrowDirections = UIPopoverArrowDirectionAny;
            popPC.delegate=self;
            [self presentViewController:imgPicker animated:false completion:nil];
        }else{
            [SVProgressHUD showErrorWithStatus:@"請前往設置界面允許程序訪問攝像頭!"];
        }
    }];

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM