2015-05-22 21 views
-2

我使用下面的代码为用户从他们的照片库中选择一个图像。 它适用于iPhone,但在iPad上,用户界面看起来与iPhone相同。它不应该是不同的?我怎么使它看起来像这样... Image Link如何使用swift更好地选择iPad上的照片?

这里是我的代码...

import UIKit 

class PostExperienceViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate { 


    @IBOutlet weak var ImageView: UIImageView! 


    override func viewDidLoad() { 
     super.viewDidLoad() 

     // Do any additional setup after loading the view. 
    } 

    override func didReceiveMemoryWarning() { 
     super.didReceiveMemoryWarning() 
     // Dispose of any resources that can be recreated. 
    } 




    @IBAction func segmentedControll(sender: UISegmentedControl) { 

     let segmentedControl = sender as UISegmentedControl 

     switch segmentedControl.selectedSegmentIndex { 

     case 0: 
      //photo library 
      if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.PhotoLibrary) { 

       let imagePicker:UIImagePickerController = UIImagePickerController() 
       imagePicker.delegate = self 
       imagePicker.sourceType = UIImagePickerControllerSourceType.PhotoLibrary 
       imagePicker.allowsEditing = false 

       self.presentViewController(imagePicker, animated: true, completion: nil) 




      } 


     case 1: 
      //camera 
      if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.Camera) { 

       let imagePicker:UIImagePickerController = UIImagePickerController() 
       imagePicker.delegate = self 
       imagePicker.sourceType = UIImagePickerControllerSourceType.Camera 
       imagePicker.allowsEditing = false 

       self.presentViewController(imagePicker, animated: true, completion: nil) 




      } 


     default: 
      break; 

     } 

    } 




    func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [NSObject : AnyObject]) { 


     self.dismissViewControllerAnimated(true, completion: nil) 

     self.ImageView.image = info[UIImagePickerControllerOriginalImage] as? UIImage 

    } 
+0

的'UIImagePickerController'文档指示我们如果从图书馆中选择一张照片,应该在iPad上使用popover。 – Rob

+0

但是如何?我使用哪些代码? –

回答

1

,我发现自己的答案...

if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.PhotoLibrary) { 

       let imagePicker:UIImagePickerController = UIImagePickerController() 
       imagePicker.delegate = self 
       imagePicker.sourceType = UIImagePickerControllerSourceType.PhotoLibrary 
       imagePicker.allowsEditing = false 

if UIDevice.currentDevice().userInterfaceIdiom == .Phone { 
        self.presentViewController(imagePicker, animated: true, completion: nil) 
       } 
       else { 
        imagePicker.modalPresentationStyle = .Popover 
        presentViewController(imagePicker, animated: true, completion: nil)//4 
        imagePicker.popoverPresentationController?.sourceView = (sender as UISegmentedControl) 

        imagePicker.popoverPresentationController?.permittedArrowDirections = UIPopoverArrowDirection.Up 
        imagePicker.popoverPresentationController?.sourceRect = CGRect(x: 150, y: 150, width: 0, height: 0) 
       } 
} 
相关问题