xcode 以 ActionSheet 样式访问图片库

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31477657/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 07:30:21  来源:igfitidea点击:

Accessing picture library in an ActionSheet style

xcodeswiftuiimageviewactionsheetpicker

提问by Martin Q

I'm trying to create a profilePic in a Sign Up View Controller. The profilePic is of type UIImageView. I want to be able to tap it so it instantly pulls up a photo library in an ActionSheet Style. And to also have one of the image box a button to access the camera. Is this possible?

我正在尝试在注册视图控制器中创建一个 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")
        }
    }

回答by iRiziya

Try this code below :

试试下面的代码:

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)

}
}

Its working on my side..hope will do the some for you !

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

回答by Maor

I recently published a GitHub repository to handle this type of Action Sheet.

我最近发布了一个 GitHub 存储库来处理这种类型的操作表。

You can download this class from here : MSActionSheet

你可以从这里下载这个类:MSActionSheet

and simply write :

并简单地写:

EDIT :

编辑 :

● Now supporting iPad

● 现已支持iPad

MSActionSheet(viewController: self, sourceView: sender).showFullActionSheet {
       sender.setImage(
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)
    }
}
, for: .normal) }

MSActionSheet

MSActionSheet

回答by kuldip bhalodiya

MSActionSheet: is the best library for set user's profile picture.

MSActionSheet:是设置用户个人资料图片的最佳库。

  • I used this library but it's not supported iniPadso i customise for some functions for support to iPad and Display with popOverController.
  • 我使用了这个库,iPad不支持它,所以我自定义了一些功能,以支持 iPad 和使用popOverController显示 。

Below the step of customisation.

下面是定制步骤。

  1. Replace function with showFullActionSheet

    if UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiom.pad {
            if let presenter = MSActionSheet.sheet?.popoverPresentationController {
                presenter.sourceView = btn
                presenter.sourceRect = btn.bounds
            }
        }
    
  2. Add code before vc.present(MSActionSheet.sheet!, animated: true, completion: nil)in function show

    if UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiom.pad {
            picker.modalPresentationStyle = .popover
            if let presenter = picker.popoverPresentationController {
                presenter.sourceView = sourceBtn
                presenter.sourceRect = sourceBtn!.bounds
            }
        }
    
  3. Add below code before clientVC?.present(picker, animated: true, completion: nil)in function handler

    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)
        }
    }
    
  1. 将函数替换为 showFullActionSheet

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

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

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


  1. How to use

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

     class yourClassName:UIActionSheetDelegate,UIImagePickerControllerDelegate,UINavigationControllerDelegate {
    
    
     @IBAction func yourButtonName(_ sender: UIButton) {
    
    
            let pickerView = UIImagePickerController()
            pickerView.delegate = self
            pickerView.allowsEditing = true
    
    
            let actionSheetControllerIOS8: UIAlertController = UIAlertController(title: "Please select", message: nil, preferredStyle: .actionSheet)
    
            let cancelActionButton: UIAlertAction = UIAlertAction(title: "Cancel", style: .cancel) { action -> Void in
                print("Cancel") //Cancel
            }
            actionSheetControllerIOS8.addAction(cancelActionButton)
    
            let saveActionButton: UIAlertAction = UIAlertAction(title: "Take Photo", style: .default) { action -> Void in
    
                print("Take Photo") //Will open Camera
                if UIImagePickerController.isSourceTypeAvailable(.camera) {
    
                    pickerView.sourceType = .camera
                    self.present(pickerView, animated: true, completion: { _ in })
                }
    
            }
            actionSheetControllerIOS8.addAction(saveActionButton)
    
            let deleteActionButton: UIAlertAction = UIAlertAction(title: "Camera Roll", style: .default) { action -> Void in
    
                print("Camera Roll") //Will open Gallery
    
                pickerView.sourceType = .photoLibrary
                self.present(pickerView, animated: true, completion: { _ in })
    
            }
            actionSheetControllerIOS8.addAction(deleteActionButton)
    
            let deleteActionButton2: UIAlertAction = UIAlertAction(title: "Import from Facebook", style: .default) { action -> Void in
                print("Import from Facebook")
            }
    
            actionSheetControllerIOS8.addAction(deleteActionButton2)
            self.present(actionSheetControllerIOS8, animated: true, completion: nil)
    
        }
    
    // MARK: - UIImagePickerControllerDelegate Methods
    
    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
    
    //print(info)
    if let pickedImage = info[UIImagePickerControllerEditedImage] as? UIImage {
    
        //print(pickedImage)
    
        yourImageView.contentMode = .scaleAspectFit
        yourImageView.image = pickedImage
    
    }
    dismiss(animated: true, completion: nil)
    }
    
    }
    

回答by Anurag Sharma

Swift 3x:

斯威夫特 3 倍:

##代码##