微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Swift_UIImagePickerController选择图片显示

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.
    }
    */
    
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐