-
Notifications
You must be signed in to change notification settings - Fork 0
/
ViewController.swift
142 lines (90 loc) · 4.21 KB
/
ViewController.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
//
// ViewController.swift
// ImagePickView
//
// Created by lengshengren on 15/10/9.
// Copyright © 2015年 Apress. All rights reserved.
//
import UIKit
import MobileCoreServices
class ViewController: UIViewController ,UIImagePickerControllerDelegate,UINavigationControllerDelegate,UIActionSheetDelegate {
@IBOutlet weak var imageView: UIImageView!
var imagePicker : UIImagePickerController!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func selectImageAction(sender: AnyObject) {
let actionSheetController: UIAlertController = UIAlertController(title: "请选择", message:nil, preferredStyle: .ActionSheet)
//取消按钮
let cancelAction: UIAlertAction = UIAlertAction(title: "取消", style: .Cancel) { action -> Void in
//Just dismiss the action sheet
}
actionSheetController.addAction(cancelAction)
//拍照
let takePictureAction: UIAlertAction = UIAlertAction(title: "拍照", style: .Default)
{ action -> Void in
[self .initWithImagePickView("拍照")];
}
actionSheetController.addAction(takePictureAction)
//相册选择
let choosePictureAction: UIAlertAction = UIAlertAction(title: "相册", style: .Default)
{ action -> Void in
[self .initWithImagePickView("相册")];
}
actionSheetController.addAction(choosePictureAction)
//摄像
let moviePictureAction: UIAlertAction = UIAlertAction(title: "摄像", style: .Default)
{ action -> Void in
[self .initWithImagePickView("摄像")];
}
actionSheetController.addAction(moviePictureAction)
self.presentViewController(actionSheetController, animated: true, completion: nil)
}
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
let mediaType = info[UIImagePickerControllerMediaType] as! String
let compareResult = CFStringCompare(mediaType as NSString!, kUTTypeMovie, CFStringCompareFlags.CompareCaseInsensitive)
if compareResult == CFComparisonResult.CompareEqualTo {
let moviePath = info[UIImagePickerControllerMediaURL] as? NSURL
//获取路径
let moviePathString = moviePath!.relativePath
if UIVideoAtPathIsCompatibleWithSavedPhotosAlbum(moviePathString!)
{
UISaveVideoAtPathToSavedPhotosAlbum(moviePathString!, nil, nil, nil)
}
print("视频")
}
else {
print("图片")
let image = info[UIImagePickerControllerOriginalImage] as? UIImage
self.imageView.image = image;
}
imagePicker.dismissViewControllerAnimated(true, completion: nil)
}
func initWithImagePickView(type:NSString){
self.imagePicker = UIImagePickerController()
self.imagePicker.delegate = self;
self.imagePicker.allowsEditing = true;
switch type{
case "拍照":
self.imagePicker.sourceType = .Camera
break
case "相册":
self.imagePicker.sourceType = .PhotoLibrary
break
case "摄像":
self.imagePicker.sourceType = .Camera
self.imagePicker.videoMaximumDuration = 60 * 3
self.imagePicker.videoQuality = .Type640x480
self.imagePicker.mediaTypes = [String(kUTTypeMovie)]
break
default:
print("error")
}
presentViewController(self.imagePicker, animated: true, completion: nil)
}
}