爱程序网

跟我学swift 项目经验 第一天。

来源: 阅读:

swift语言的语法我这里是不太会讲了 主要是介绍一下一些经常用到的功能模块: 今天我们来学习一下用swift来进行拍照 明天在写 下班了

看代码:

//
//  CaptureViewController.swift
//  com.kaiyuanhotels.webapp
//
//  Created by 富森江 on 15-2-3.
//  Copyright (c) 2015年 富森江. All rights reserved.
//

import UIKit
import MobileCoreServices

class CaptureViewController: UIViewController,UIImagePickerControllerDelegate,UINavigationControllerDelegate {

    @IBOutlet weak var imageview1: UIImageView!
    @IBOutlet weak var imageview2: UIImageView!
    var selectmode = NSString()
    override func viewDidLoad() {
        super.viewDidLoad()
    }
    
    func cameraSupportsMedia(mediaType: String,
        sourceType: UIImagePickerControllerSourceType) -> Bool{
            let availableMediaTypes =
            UIImagePickerController.availableMediaTypesForSourceType(sourceType)
                as [String]
            for type in availableMediaTypes{
                if type == mediaType{
                    return true
                }
            }
            return false
    }
    
    func doesCameraSupportShootingVideos() -> Bool{
        return cameraSupportsMedia(kUTTypeMovie as NSString, sourceType: .Camera)
    }
    
    func doesCameraSupportTakingPhotos() -> Bool{
        return cameraSupportsMedia(kUTTypeImage as NSString, sourceType: .Camera)
    }
    
    @IBAction func capturephoto(sender: AnyObject) {
        if (UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.Camera) && doesCameraSupportTakingPhotos()) {
            let imagePicker = UIImagePickerController()
            imagePicker.allowsEditing = false
            imagePicker.delegate = self
            imagePicker.sourceType = .Camera
            selectmode = "camera"
            self.presentViewController(imagePicker, animated: true, completion: nil)
        }
    }
    
    @IBAction func photolibrary(sender: AnyObject) {
        if (UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.PhotoLibrary)) {
            let imagePicker = UIImagePickerController()
            imagePicker.allowsEditing = false
            imagePicker.delegate = self
            imagePicker.sourceType = .PhotoLibrary
            selectmode = "library"
            self.presentViewController(imagePicker, animated: true, completion: nil)
        }
    }
    
    func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [NSObject : AnyObject]) {
        if(selectmode == "camera"){
            imageview1.image = info[UIImagePickerControllerOriginalImage] as? UIImage
            imageview1.contentMode = UIViewContentMode.ScaleAspectFill
            imageview1.clipsToBounds = true
        }else{
            imageview2.image = info[UIImagePickerControllerOriginalImage] as? UIImage
            imageview2.contentMode = UIViewContentMode.ScaleAspectFill
            imageview2.clipsToBounds = true
        }
        dismissViewControllerAnimated(true, completion: nil)
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

}

  

关于爱程序网 - 联系我们 - 广告服务 - 友情链接 - 网站地图 - 版权声明 - 人才招聘 - 帮助