xcode – 以ActionSheet样式访问图片库
发布时间:2020-12-14 18:57:53 所属栏目:百科 来源:网络整理
导读:我正在尝试在注册视图控制器中创建profilePic. profilePic的类型为UI ImageView.我希望能够点击它,以便立即在ActionSheet样式中提取照片库.并且还有一个图像框一个按钮来访问相机.这可能吗? import UIKitclass SignUpViewController: UIViewController,UITe
我正在尝试在注册视图控制器中创建profilePic. profilePic的类型为UI
ImageView.我希望能够点击它,以便立即在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,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",handler: { (alert:UIAlertAction!) -> Void in self.photoLibrary() })) actionSheet.addAction(UIAlertAction(title: "Cancel",style: UIAlertActionStyle.Cancel,handler: nil)) self.presentViewController(actionSheet,completion: nil) } func imagePickerController(picker: UIImagePickerController,didFinishPickingMediaWithInfo info: [NSObject : AnyObject]) { profilePic.image = info[UIImagePickerControllerOriginalImage] as? UIImage self.dismissViewControllerAnimated(true,completion: nil) } } 它在我身边工作..希望会为你做一些! (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |