加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

Swift_UIImagePickerController选择图片显示

发布时间:2020-12-14 01:46:54 所属栏目:百科 来源:网络整理
导读:Swift_UIImagePickerController选择图片显示代码如下: import UIKitclass ViewController: UIViewController,UIImagePickerControllerDelegate,UINavigationControllerDelegate { //跳转图库按钮 var selectButton:UIButton! //显示图片 var imageView:UIIm

Swift_UIImagePickerController选择图片显示代码如下:

import UIKit

class ViewController: UIViewController,UIImagePickerControllerDelegate,UINavigationControllerDelegate {
    
    //跳转图库按钮
    var selectButton:UIButton!
    //显示图片
    var imageView:UIImageView!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        //创建子视图
        createSubViews()
    }
    
    //MARK: - 创建子视图
    func createSubViews() {
        
        self.imageView = UIImageView(frame: CGRectMake(80,100,200,200))
        self.imageView.backgroundColor = UIColor.blueColor()
        self.view.addSubview(self.imageView)
        
        self.selectButton = UIButton(type: UIButtonType.Custom)
        self.selectButton.frame = CGRectMake(160,350,80,40)
        self.selectButton.backgroundColor = UIColor.purpleColor()
        self.selectButton.setTitle("选择图片",forState: UIControlState.Normal)
        self.selectButton.setTitleColor(UIColor.redColor(),forState: UIControlState.Normal)
        self.selectButton.titleLabel?.font = UIFont.boldSystemFontOfSize(18)
        //self.selectButton.addTarget(self,action: Selector(selectImageAction(self.selectButton)),forControlEvents: UIControlEvents.TouchUpInside)
        //按钮的action:方法
        self.selectButton.addTarget(self,action: "selectImageAction:",forControlEvents: UIControlEvents.TouchUpInside)
        self.view.addSubview(self.selectButton)
        
    }
    //MARK: buttonAction
    func selectImageAction(sender: AnyObject) {
        
        let imagePC:UIImagePickerController = UIImagePickerController()
        imagePC.sourceType = UIImagePickerControllerSourceType.PhotoLibrary
        imagePC.delegate = self
        self.presentViewController(imagePC,animated: true,completion: nil)
        print("button click")
        
    }
    //MARK: - UIImagePickerControllerDelegate
    func imagePickerController(picker: UIImagePickerController,didFinishPickingMediaWithInfo info: [String : AnyObject]) {
        
        //print("info = %@",info)
        print("info = (info)")
        /*
        UIImagePickerControllerOriginalImage,<UIImage: 0x7aedc800> size {4288,2848} orientation 0 scale 1.000000
        UIImagePickerControllerReferenceURL,assets-library://asset/asset.JPG?id=7FECF4BC-7660-4CC6-8BD2-39443E4A4181&ext=JPG
        UIImagePickerControllerMediaType,public.image
        for (a,b) in info {
        print("(a),(b)")
        }
        */
        let img = info["UIImagePickerControllerOriginalImage"] as! UIImage
        self.imageView.image = img
        picker.dismissViewControllerAnimated(true,completion: nil)
    }
    internal func imagePickerControllerDidCancel(picker: UIImagePickerController) {
        picker.dismissViewControllerAnimated(true) { () -> Void in
            print("dismiss ok")
        }
    }
    
    /*
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    */
    
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读