以 ActionSheet 样式访问图片库

Accessing picture library in an ActionSheet style

我正在尝试在注册视图控制器中创建一个 profilePic。 profilePic 是 UIImageView 类型。我希望能够点击它,以便它立即以 ActionSheet 样式调出照片库。并且还有一个图像框按钮来访问相机。这可能吗?

import UIKit

class SignUpViewController: UIViewController, UITextFieldDelegate, UIPickerViewDataSource, UIPickerViewDelegate {

    @IBOutlet weak var profilePic: UIImageView!


    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.

        // PROFILE PICTURE

        let tapGesture = UITapGestureRecognizer(target: self, action: "imageTapped:")

        profilePic.addGestureRecognizer(tapGesture)
        profilePic.userInteractionEnabled = true

    }


    func imageTapped(gesture:UIGestureRecognizer) {
        if let profile1Pic = gesture.view as? UIImageView {
            print("Image Tapped")
        }
    }

试试下面的代码:

import UIKit

class SignUpViewController: UIViewController, UIImagePickerControllerDelegate {

@IBOutlet weak var profilePic: UIImageView!


override func viewDidLoad() {
  super.viewDidLoad()

  let tapGesture = UITapGestureRecognizer(target: self, action: "imageTapped:")

  profilePic.addGestureRecognizer(tapGesture)
  profilePic.userInteractionEnabled = true

}


func imageTapped(gesture:UIGestureRecognizer) {
  if let profile1Pic = gesture.view as? UIImageView {
      print("Image Tapped")
      showActionSheet()
  }
}
func camera()
{
    var myPickerController = UIImagePickerController()
    myPickerController.delegate = self;
    myPickerController.sourceType = UIImagePickerControllerSourceType.Camera

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

}

func photoLibrary()
{

    var myPickerController = UIImagePickerController()
    myPickerController.delegate = self;
    myPickerController.sourceType = UIImagePickerControllerSourceType.PhotoLibrary

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

}

func showActionSheet() {
    let actionSheet = UIAlertController(title: nil, message: nil, preferredStyle: UIAlertControllerStyle.ActionSheet)

    actionSheet.addAction(UIAlertAction(title: "Camera", style: UIAlertActionStyle.Default, handler: { (alert:UIAlertAction!) -> Void in
        self.camera()
    }))

    actionSheet.addAction(UIAlertAction(title: "Gallery", style: UIAlertActionStyle.Default, handler: { (alert:UIAlertAction!) -> Void in
        self.photoLibrary()
    }))

    actionSheet.addAction(UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Cancel, handler: nil))

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

}

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

    profilePic.image = info[UIImagePickerControllerOriginalImage] as? UIImage
    self.dismissViewControllerAnimated(true, completion: nil)

}
}

它在我这边工作..希望能为你做一些!

È=12=ÈтÈ=13=ÈтÈ=15=Èт zks:È=14=ÈтÈ=11=Èт w=10=sh

我最近发布了一个 GitHub 存储库来处理此类操作 Sheet。

您可以从此处下载此 class:MSActionSheet

然后简单地写:

编辑:

● 现已支持iPad

MSActionSheet(viewController: self, sourceView: sender).showFullActionSheet {
       sender.setImage([=10=], for: .normal)
}

MSActionSheet : 是设置用户头像的最佳库。

  • 我使用了这个库但是不支持它iPad所以我 自定义一些功能以支持 iPad 和 Display with popOverController.

自定义步骤以下

  1. 将函数替换为showFullActionSheet

    func showFullActionSheet(on vc : UIViewController, over btn : UIButton,handler : @escaping (_ : UIImage?) -> Void) {
        let sheet = create().addLibrary().addFrontCamera().addRearCamera().addCancelButton()
        sheet.show(on: vc,over: btn){ (image : UIImage?) in
            handler(image)
        }
    }
    
  2. 在函数show

    vc.present(MSActionSheet.sheet!, animated: true, completion: nil)之前添加代码
    if UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiom.pad {
            if let presenter = MSActionSheet.sheet?.popoverPresentationController {
                presenter.sourceView = btn
                presenter.sourceRect = btn.bounds
            }
        }
    
  3. 在函数handler

    clientVC?.present(picker, animated: true, completion: nil)之前添加以下代码
    if UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiom.pad {
            picker.modalPresentationStyle = .popover
            if let presenter = picker.popoverPresentationController {
                presenter.sourceView = sourceBtn
                presenter.sourceRect = sourceBtn!.bounds
            }
        }
    

  1. 使用方法

    let msActionSheet = MSActionSheet.instance
        msActionSheet.showFullActionSheet(on: self,over: btn){ (image) in
            btn.setImage(image, for: .normal)
        }
        msActionSheet.tintColor(color: Constants.kColorBluish)