photopickerviewController

//
// PhotoPickerViewController.swift
// XBook
//
// Created by xlx on 15/12/11.
// Copyright © 2015年 xlx. All rights reserved.
//

import UIKit

protocol PhotoPickerDelegate{
func getImageFromPicker(image:UIImage)
}

class PhotoPickerViewController: UIViewController,UIImagePickerControllerDelegate,UINavigationControllerDelegate{

var alert:UIAlertController?

var picker:UIImagePickerController!

var delegate:PhotoPickerDelegate!


init(){
    super.init(nibName: nil, bundle: nil)
    self.modalPresentationStyle = .OverFullScreen
    
    self.view.backgroundColor = UIColor.clearColor()
    
    
    self.picker = UIImagePickerController()
    self.picker.allowsEditing = false
    self.picker.delegate = self
    
    
    
}
override func viewDidLoad() {
    super.viewDidLoad()

}
required init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not implemeted")
}
override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
}

override func viewDidAppear(animated: Bool) {
    if(self.alert == nil){
        self.alert = UIAlertController(title: nil, message: nil, preferredStyle: .ActionSheet)
        self.alert?.addAction(UIAlertAction(title: "从相册选择", style: .Default, handler: { (action) -> Void in
            self.localPhoto()
        }))
        self.alert?.addAction(UIAlertAction(title: "打开相机", style: .Default, handler: { (action) -> Void in
           self.takePhoto()
        }))
        self.alert?.addAction(UIAlertAction(title: "取消", style: .Cancel, handler: { (action) -> Void in
            
        }))
        self.presentViewController(self.alert!, animated: true, completion: { () -> Void in
            
        })
    }
}
/**
 打开相机
 */
func takePhoto(){
    if(UIImagePickerController.isSourceTypeAvailable(.Camera)){
        
        self.picker.sourceType = .Camera
        self.presentViewController(self.picker, animated: true, completion: { () -> Void in
            
        })
        
    
    
    }else{
        let alertView = UIAlertController(title: "此机型无相机", message: nil, preferredStyle: .Alert)
        alertView.addAction(UIAlertAction(title: "关闭", style: .Cancel, handler: { (action) -> Void in
            self.dismissViewControllerAnimated(true, completion: { () -> Void in
                
            })
            
        }))
        self.presentViewController(alertView, animated: true, completion: { () -> Void in
            
        })
    }
}
/**
*  打开相册
*/
func localPhoto(){
    self.picker.sourceType = .PhotoLibrary
    self.presentViewController(self.picker, animated: true) { () -> Void in
        
    }

}

func imagePickerControllerDidCancel(picker: UIImagePickerController) {
    self.picker.dismissViewControllerAnimated(true) { () -> Void in
        self.dismissViewControllerAnimated(true, completion: { () -> Void in
            
        })
    }
}
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
    let image = info[UIImagePickerControllerOriginalImage] as! UIImage
    self.picker.dismissViewControllerAnimated(true) { () -> Void in
        self.dismissViewControllerAnimated(true, completion: { () -> Void in
            self.delegate.getImageFromPicker(image)
        })
    }
}

}

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 总结:页面的跳转,以及模态。 //一、ViewController.swift: // // ViewContro...
    冰凡513阅读 364评论 0 0
  • 在iOS中随处都可以看到绚丽的动画效果,实现这些动画的过程并不复杂,今天将带大家一窥iOS动画全貌。在这里你可以看...
    F麦子阅读 5,147评论 5 13
  • 在iOS中随处都可以看到绚丽的动画效果,实现这些动画的过程并不复杂,今天将带大家一窥ios动画全貌。在这里你可以看...
    每天刷两次牙阅读 8,573评论 6 30
  • How To Use OC Block Part1 Block简述 Block作为语言特性 在Mac OS X 1...
    柳豪阅读 1,671评论 0 50
  • 我的小王老师已经可以穿着轮滑鞋慢慢走了! 倔强的小家伙战胜了自己,真的很棒!特别棒! 小王老师对妈妈说:“宝宝进步...
    高小花0218阅读 173评论 0 0