Swift_UIImagePickerController选择图片显示

前端之家收集整理的这篇文章主要介绍了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.
    }
    */
    
}
原文链接:https://www.f2er.com/swift/325844.html

猜你在找的Swift相关文章