【问题标题】:how to make selecting a photo on an iPad look better using swift?如何使用 swift 在 iPad 上选择照片看起来更好?
【发布时间】:2015-05-22 13:29:14
【问题描述】:

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

这是我的代码...

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

    }

【问题讨论】:

  • UIImagePickerController 文档告诉我们,如果从图库中挑选一张照片,应该在 iPad 上使用 popover。
  • 但是怎么做呢?我用什么代码?

标签: ios swift ipad uiimagepickercontroller


【解决方案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)
                }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-06-18
    • 1970-01-01
    • 1970-01-01
    • 2018-01-12
    • 2011-09-12
    • 1970-01-01
    • 2018-01-25
    • 2013-07-23
    相关资源
    最近更新 更多