Swift_UIImagePickerController选择图片显示
2015-09-30 10:51
375 查看
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. } */ }
相关文章推荐
- Swift学习- 方法(十一)
- 2.Swift初探-基本语法
- 《从零开始学Swift》学习笔记(Day 19)——函数参数传递
- 《从零开始学Swift》学习笔记(Day 19)——函数参数传递
- swift基础篇:视图控制器UIViewControler
- swift关于UIView设置frame值的extension
- iOS swift-2 (元组)
- iOS swift-1
- 《从零开始学Swift》学习笔记(Day 18)——有几个分支语句?
- Swift学习-属性(十)
- Swift编程语言学习1.3——类型安全和投机型
- Swift学习-类和结构体(九)
- Swift数据类型-可选类型
- Swift 2.0实战:如何实现从非零下标遍历数组?
- Swift - 后台获取数据(Background Fetch)的实现
- Swift - 让程序挂起后,能在后台继续运行任务
- sqlite3数据库增删改查通用类swift
- Swift中的闭包
- Swift函数与类中方法的参数标签
- Swift 2.0中的数据类型