xcode – 以ActionSheet样式访问图片库

xcode – 以ActionSheet样式访问图片库,第1张

概述我正在尝试在注册视图控制器中创建profilePic. profilePic的类型为UI ImageView.我希望能够点击它,以便立即在ActionSheet样式中提取照片库.并且还有一个图像框一个按钮来访问相机.这可能吗? import UIKitclass SignUpViewController: UIViewController, UITextFieldDelegate, UIPic 我正在尝试在注册视图控制器中创建profilePic. profilePic的类型为UI ImageVIEw.我希望能够点击它,以便立即在ActionSheet样式中提取照片库.并且还有一个图像框一个按钮来访问相机.这可能吗?

import UIKitclass 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 UIKitclass 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)}}

它在我身边工作..希望会为你做一些!

总结

以上是内存溢出为你收集整理的xcode – 以ActionSheet样式访问图片库全部内容,希望文章能够帮你解决xcode – 以ActionSheet样式访问图片库所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1036373.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-24
下一篇 2022-05-24

发表评论

登录后才能评论

评论列表(0条)

保存