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


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值