swift语言IOS8开发战记19 UIImagePickerController

简介:   我们接着上一话的内容来讲,首先在我们添加一个餐馆的页面中把点击的背景色取消掉使用的是以下语句:cell.selectionStyle = UITableViewCellSelectionStyle.None除了none之外,还有许多颜色的选项,大家可以自己试试。

  我们接着上一话的内容来讲,首先在我们添加一个餐馆的页面中把点击的背景色取消掉使用的是以下语句:

cell.selectionStyle = UITableViewCellSelectionStyle.None

除了none之外,还有许多颜色的选项,大家可以自己试试。然后我们需要点击小相机的图片能打开我们的相册让我们选择图片,我们在选择的代理方法中增加一个我们自己定义的方法,代码如下:

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {//点击行的代理方法
        var row:NSInteger = indexPath.row
        if row == 0{
        self.didClickedImageView()
        }
下面是didClickedImageView的代码:
func didClickedImageView(){
    let imagePicker = UIImagePickerController() //选择图片
    let isAvailable = UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.Camera)
        imagePicker.sourceType = .PhotoLibrary
        if isAvailable {
            imagePicker.sourceType = .Camera
        }
        self.presentViewController(imagePicker, animated: true, completion: nil)
    }

点击小相机的图标显示如下:


现在我们希望我们选择了照片之后可以把该照片保存到第一行之中,这就需要在点击第一行的时候把当前的类当做imagePicker的代理,所以把点击第一行所触发的方法didClickedImageView修改如下:

func didClickedImageView(){
    let imagePicker = UIImagePickerController() //选择图片
    let isAvailable = UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.Camera)
        imagePicker.sourceType = .PhotoLibrary
        imagePicker.delegate = self//代理设为自己
        if isAvailable {
            imagePicker.sourceType = .Camera
        }
        self.presentViewController(imagePicker, animated: true, completion: nil)
    }

imagePicker是UIImagePickerController类型的,要实现这个类型的代理,必须继承
UINavigationControllerDelegateUIImagePickerControllerDelegate,然后我们实现代理方法imagePickerController用来保存我们选择的图片,代码如下:

 func imagePickerController(picker: UIImagePickerController!, didFinishPickingImage image: UIImage!, editingInfo: [NSObject : AnyObject]!) {//保存图片
        imageView.image = image
            picker.dismissViewControllerAnimated(true, completion: nil)
        }

在选中图片后,效果如图



接下来我们来实现下面这几行,代码如下:

 func configureCell(cell:UITableViewCell, indexPath: NSIndexPath){
        let row:NSInteger = indexPath.row
        var nameLabel = UILabel(frame: CGRectMake(10, 5, 200, 15))
            switch row{
        case 0:
         imageView = UIImageView(frame: CGRectMake(0, 0, 320, 250))
            imageView.image = UIImage(named: "camera")
            imageView.contentMode = UIViewContentMode.ScaleAspectFit
            cell.addSubview(imageView)
        case 1:
            nameLabel.text = "Name"
            cell.addSubview(nameLabel)
                var textFiled = UITextField(frame: CGRectMake(10, 20, 250, 40))
            textFiled.placeholder = "Restaurant Name"
                cell.addSubview(textFiled)
        case 2:
            nameLabel.text = "Type"
            cell.addSubview(nameLabel)
            var textFiled = UITextField(frame: CGRectMake(10, 20, 250, 40))
            textFiled.placeholder = "Restaurant Type"
            cell.addSubview(textFiled)
            
        case 3:
            nameLabel.text = "Location"
            cell.addSubview(nameLabel)
            var textFiled = UITextField(frame: CGRectMake(10, 20, 250, 40))
            textFiled.placeholder = "Restaurant Location"
            cell.addSubview(textFiled)
        default:
            nameLabel.text = "Have you been here?"
            cell.addSubview(nameLabel)
            
            
        }
    }
结构都比较相似,我们把之前的if结构改成了switch,UITextFiled有一个属性placeholder,实现了水印字体的效果,效果如下:

在最后一栏中需要加两个按钮,一个YES和一个NO,然后再导航栏中增加一个标题,NewViewController的完整代码如下:

import UIKit

class NewViewController: UIViewController,UITableViewDataSource,UITableViewDelegate,UINavigationControllerDelegate,UIImagePickerControllerDelegate  {
let identifier = "newCell"
    var imageView:UIImageView!
    override func viewDidLoad() {
        super.viewDidLoad()
self.view.backgroundColor = UIColor.orangeColor()
        
        //增加一个tableview
        var tableView = UITableView(frame: self.view.bounds, style: .Plain)
        tableView.dataSource = self
        tableView.delegate = self
        tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "newCell")
        self.view.addSubview(tableView)
        self.navigationItem.title = "New Restaurant"
        
    }
    func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return 1
    }
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 5
    }
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        
        let cell = UITableViewCell(style: .Default, reuseIdentifier: "newCell")
        self.configureCell(cell, indexPath: indexPath)
        cell.selectionStyle = UITableViewCellSelectionStyle.None//选中的背景色
        return cell
    }
    func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
        var row:NSInteger = indexPath.row
        var rowHeight:CGFloat = 62
        if row == 0
        {
        rowHeight = 250
        }
        return rowHeight
    }
    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        var row:NSInteger = indexPath.row
        if row == 0{
        self.didClickedImageView()
        }
    }
    
    func configureCell(cell:UITableViewCell, indexPath: NSIndexPath){
        let row:NSInteger = indexPath.row
        var nameLabel = UILabel(frame: CGRectMake(10, 5, 200, 15))
            switch row{
        case 0:
         imageView = UIImageView(frame: CGRectMake(0, 0, 320, 250))
            imageView.image = UIImage(named: "camera")
            imageView.contentMode = UIViewContentMode.ScaleAspectFit
            cell.addSubview(imageView)
        case 1:
            nameLabel.text = "Name"
            cell.addSubview(nameLabel)
                var textFiled = UITextField(frame: CGRectMake(10, 20, 250, 40))
            textFiled.placeholder = "Restaurant Name"
                cell.addSubview(textFiled)
        case 2:
            nameLabel.text = "Type"
            cell.addSubview(nameLabel)
            var textFiled = UITextField(frame: CGRectMake(10, 20, 250, 40))
            textFiled.placeholder = "Restaurant Type"
            cell.addSubview(textFiled)
            
        case 3:
            nameLabel.text = "Location"
            cell.addSubview(nameLabel)
            var textFiled = UITextField(frame: CGRectMake(10, 20, 250, 40))
            textFiled.placeholder = "Restaurant Location"
            cell.addSubview(textFiled)
        default:
            nameLabel.text = "Have you been here?"
            cell.addSubview(nameLabel)
            var leftBtn = UIButton(frame: CGRectMake(10, 30, 50, 25))
            leftBtn.setTitle("YES", forState: .Normal)
                leftBtn.setTitleColor(UIColor.whiteColor(), forState: .Normal)
                leftBtn.backgroundColor = UIColor.redColor()
                cell.addSubview(leftBtn)
            var rightBtn = UIButton(frame: CGRectMake(90, 30, 50, 25))
            rightBtn.setTitle("NO", forState: .Normal)
            rightBtn.setTitleColor(UIColor.whiteColor(), forState: .Normal)
            rightBtn.backgroundColor = UIColor.grayColor()
                cell.addSubview(rightBtn)
            
        }
    }
    
    func didClickedImageView(){
    let imagePicker = UIImagePickerController() //选择图片
    let isAvailable = UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.Camera)
        imagePicker.sourceType = .PhotoLibrary
        imagePicker.delegate = self
        if isAvailable {
            imagePicker.sourceType = .Camera
        }
        self.presentViewController(imagePicker, animated: true, completion: nil)
    }
    
    func imagePickerController(picker: UIImagePickerController!, didFinishPickingImage image: UIImage!, editingInfo: [NSObject : AnyObject]!) {//保存图片
        imageView.image = image
            picker.dismissViewControllerAnimated(true, completion: nil)
        }
    
}




目录
相关文章
|
7月前
|
API Swift iOS开发
回顾Apple Swift语言的发展和版本演变历程
归纳起来,Swift的发展过程就像一个乡村孩子的成长故事,从一个不谙世事的孩子,通过不懈的学习和实践,让自身的潜力得到最大的发挥,最终成为了社会中有实力的一份子。而这个故事还在继续,让我们共同期待Swift未来的激动人心的篇章!
188 20
|
机器学习/深度学习 TensorFlow Swift
Swift语言适合多个领域的开发
Swift语言适合多个领域的开发
355 9
|
安全 编译器 Swift
Swift开发
Swift开发
358 9
|
监控 安全 Swift
企业上网行为管理软件:Swift 语言在移动终端监控的拓展
在数字化时代,企业对员工移动终端的上网行为管理日益重视。Swift 语言在移动终端监控中展现出独特优势,包括网络状态监测、应用使用跟踪及网页浏览行为监控等功能,有效助力企业确保信息安全和提高工作效率。
207 6
|
机器学习/深度学习 人工智能 移动开发
Swift语言作为苹果公司推出的现代编程语言
Swift语言作为苹果公司推出的现代编程语言
252 8
|
安全 开发工具 Swift
Swift 是苹果公司开发的现代编程语言,具备高效、安全、简洁的特点,支持类型推断、闭包、泛型等特性,广泛应用于苹果各平台及服务器端开发
Swift 是苹果公司开发的现代编程语言,具备高效、安全、简洁的特点,支持类型推断、闭包、泛型等特性,广泛应用于苹果各平台及服务器端开发。基础语法涵盖变量、常量、数据类型、运算符、控制流等,高级特性包括函数、闭包、类、结构体、协议和泛型。
382 2
|
安全 Swift iOS开发
Swift 与 UIKit 在 iOS 应用界面开发中的关键技术和实践方法
本文深入探讨了 Swift 与 UIKit 在 iOS 应用界面开发中的关键技术和实践方法。Swift 以其简洁、高效和类型安全的特点,结合 UIKit 丰富的组件和功能,为开发者提供了强大的工具。文章从 Swift 的语法优势、类型安全、编程模型以及与 UIKit 的集成,到 UIKit 的主要组件和功能,再到构建界面的实践技巧和实际案例分析,全面介绍了如何利用这些技术创建高质量的用户界面。
326 2
|
机器学习/深度学习 安全 数据挖掘
Swift语言的应用场景非常广泛
Swift语言的应用场景非常广泛
526 4
|
安全 Swift iOS开发
Swift语言
Swift语言
307 4
|
物联网 Android开发 iOS开发
iOS开发 - 蓝牙学习的总结
iOS开发 - 蓝牙学习的总结
317 0

相关课程

更多