1
1
# Swift视频录制开发指南
2
- ======================================================
2
+ ===
3
3
4
4
> * 原文链接 : [ Take Video Tutorial in iOS8 with Swift] ( http://www.ioscreator.com/tutorials/take-video-tutorial-ios8-swift )
5
5
* 原文作者 : [ ioscreator] ( www.ioscreator.com )
6
6
* [ 译文出自 : 开发技术前线 www.devtf.cn ] ( http://www.devtf.cn )
7
7
* 译者 : [ Mr.Simple] ( https://github.com/bboyfeiyu )
8
- * 校对者: [ ] ( )
9
-
10
- Apple provides the UIImagePickerController which is an user interface to
11
- take videos using the built-in camera of an iOS device. In this tutorial
12
- we will take a video which will be saved in the Photo Library of the
13
- device. This tutorial is built in iOS 8.4 and Xcode 6.4。
8
+ * 校对者: [ StormXX] ( https://github.com/StormXX )
14
9
15
10
Apple提供了一个UIImagePickerController用户界面类来让用户使用内置的摄像头来拍摄视频。我的开发环境是iOS 8.4和xcode 6.4,在这篇文章中我会教大家获取已经存储在Photo Library的视频。
16
11
17
- Open Xcode and create a new Single View Application. For product name,
18
- use ** IOS8SwiftTakeVideoPlayerTutorial** and then fill out the
19
- Organization Name and Organization Identifier with your customary
20
- values. Enter Swift as Language and make sure only iPhone is selected in
21
- Devices.
22
-
23
12
首先打开Xcode,然后创建一个名为IOS8SwiftTakeVideoPlayerTutorial的应用,Organization名和Organization标识符使用你自己的即可。选择Swift作为开发语言,并且该应用只支持Iphone。
24
13
25
14
![ ] ( http://static1.squarespace.com/static/52428a0ae4b0c4a5c2a2cede/t/559ce50ee4b0560c42b45099/1436345615076/ )
26
15
27
- Go to the ** Storyboard** and drag two buttons from the Object Library to
28
- the main view. Give the buttons a title of "Take Video" and "View
29
- Library". The storyboard should look like this.
30
-
31
16
到Storyboard页面,从Object Library拖两个按钮到主视图上,给这两个按钮设置title,分别为"Take Video" 和 "View Library",Storyboard此时应该如下图所示:
32
17
33
18
![ ] ( http://static1.squarespace.com/static/52428a0ae4b0c4a5c2a2cede/t/559ce595e4b0cebfa4eee243/1436345750826/ )
34
19
35
- Hold down the Ctrl key and select both buttons. Click the "Resolve Auto
36
- Layout Issues" button on the bottom-right of the Storyboard and select
37
- "Add Missing Constraints".
38
-
39
20
按住Ctrl键,然后选中这两个按钮,点击在Storyboard右下方的“Resolve Auto Layout Issues”按钮,然后选择“Add Missing Constraints”。如下图所示 :
40
21
41
-
42
22
![ ] ( http://static1.squarespace.com/static/52428a0ae4b0c4a5c2a2cede/t/559ce604e4b04c3e89ff0e2e/1436345862940/ )
43
23
44
-
45
- Select the Assistant Editor and make sure
46
- the ** ViewController.swift** is visible. Ctrl and drag from the top
47
- Button and create the following Action.
48
-
49
24
切换到Assistant Editor界面,并且确保ViewController.swift是可见的。按住Ctrl并从Take Video按钮区域拖出以创建一个Action.
50
25
51
26
![ ] ( http://static1.squarespace.com/static/52428a0ae4b0c4a5c2a2cede/t/559ce655e4b051cc74e74388/1436345942333/ )
52
27
53
-
54
- Ctrl and drag from the bottom Button and create the following Action.
55
-
56
28
同样的,按住按住Ctrl并从View Library按钮区域拖出以创建一个Action。
57
29
58
-
59
30
![ ] ( http://static1.squarespace.com/static/52428a0ae4b0c4a5c2a2cede/t/559ce67fe4b001bba4e7e8e9/1436345984380/ )
60
31
61
-
62
- Go to the ViewController.swfit file and add the following lines at the
63
- top off the file.
64
-
65
32
切换到ViewController.swfit文件,在文件的头部添加如下代码。
66
33
67
-
68
- ``` swift
34
+ ```
69
35
import MobileCoreServices
70
36
import AssetsLibrary
71
37
```
72
38
73
-
74
- Change the ViewController class declaration line to
75
-
76
39
修改ViewController类的定义如下 :
77
40
78
-
79
- ``` swift
80
- class ViewController : UIViewController , UINavigationControllerDelegate , UIImagePickerControllerDelegate {
81
- ```
82
-
83
- These delegates are needed to let the ViewController handle the
84
- UIImagePickerController delegation. Implement the ** takeVideo** method
41
+ `class ViewController: UIViewController, UINavigationControllerDelegate, UIImagePickerControllerDelegate {
42
+ `
85
43
86
44
这些delegates使得ViewController能够处理UIImagePickerController的代理事件,takeVideo的实现如下 :
87
45
88
- ```swift
46
+ ```
89
47
@IBAction func takeVideo(sender: AnyObject) {
90
48
// 1 Check if project runs on a device with camera available
91
49
if UIImagePickerController.isSourceTypeAvailable(.Camera) {
@@ -104,22 +62,12 @@ UIImagePickerController delegation. Implement the **takeVideo** method
104
62
}
105
63
```
106
64
107
-
108
- 1 . The isSourceTypeAvailable method checks if the device supports the
109
- Camera sourceType
110
- 2 . An ImagePickerController is displayed using the Camera Sourcetype
111
- and the Movie mediaType. The maximum length of a movie is set to 10
112
- seconds,
113
-
114
65
1 . isSourceTypeAvailable函数是检测设备的Camera是否可用;
115
66
2 . ImagePickerController用于显示Sourcetype为Camera、mediaType为Movie的视频,视频的最大长度为10秒。
116
67
117
- Implement the ** viewLibrary** method.
118
-
119
68
viewLibrary的实现 :
120
69
121
-
122
- ```swift
70
+ ```
123
71
@IBAction func viewLibrary(sender: AnyObject) {
124
72
// Display Photo Library
125
73
controller.sourceType = UIImagePickerControllerSourceType.PhotoLibrary
@@ -130,13 +78,9 @@ viewLibrary的实现 :
130
78
}
131
79
```
132
80
133
- The Photo library is displayed. If the mediaType isn't set to the movie
134
- type, the video files will not be displayed. Next, the delegate methods
135
- of the UIImagePickerControllerDelegate protocol needs to be implemented.
136
-
137
81
在调用上述代码之后,Photo library就会被显示。如果mediaType没有设置为Movie类型,视频文件就不会被显示出来。下一步就是实现UIImagePickerControllerDelegate协议的方法 :
138
82
139
- ```swift
83
+ ```
140
84
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [NSObject: AnyObject]) {
141
85
// 1
142
86
let mediaType:AnyObject? = info[UIImagePickerControllerMediaType]
@@ -166,46 +110,23 @@ func imagePickerController(picker: UIImagePickerController, didFinishPickingMedi
166
110
}
167
111
```
168
112
169
- The ** imagePickerController (\_ : didFinishPickingMediaWithInfo: )** method
170
- tells the delegate the user picked a movie. The info parameter contains
171
- the URL of the picked movie.
172
-
173
113
当用户选择了一个视频时` imagePickerController(\_:didFinishPickingMediaWithInfo:) ` 函数会被调用,info参数中会包含被选择的视频URL。
174
114
175
- 1 . The mediatype from the info dictionary is checked if it is a movie
176
- type. If this is the case the URL of the video is extracted.
177
- 2 . The writeVideoAtPathToSavedPhotosAlbum method saves the video into
178
- the photo album
179
- 3 . The ViewController is dismissed
180
-
181
-
182
115
1 . 首先会检测info dictionary中的mediatype是否是movie类型,如果是那么则会提取这个视频的URL;
183
116
2 . 调用writeVideoAtPathToSavedPhotosAlbum函数将视频存储到图片相册中;
184
117
3 . 隐藏该ViewController。
185
118
186
- Implement the ** imagePickerControllerDidCancel** method
187
-
188
119
` imagePickerControllerDidCancel ` 实现如下 :
189
120
190
- ```swift
121
+ ```
191
122
func imagePickerControllerDidCancel(picker: UIImagePickerController) {
192
123
picker.dismissViewControllerAnimated(true, completion: nil)
193
124
}
194
125
```
195
126
196
- When the user press the Cancel button, the View Controller is dismissed.
197
- Build and Run the project on a real device, since the Simulator doesn't
198
- have a camera. Choose " Take Video" to shoot the video and click Use
199
- Video. Next, choose " View Library" and the video appears in the library.
200
-
201
127
当用户按下取消按钮,该界面的View Controller就会被隐藏。因为模拟器没有摄像头,因此编译并且将该项目运行到真实的设备,点击"Take Video"按钮来拍一段视频,然后点击“Use Video”。再下一步选择“View Library”,此时该Video就会显示在Library中了。
202
128
203
129
![ TakeVideo-Device.png] ( http://static1.squarespace.com/static/52428a0ae4b0c4a5c2a2cede/t/559d107ee4b0a65ec39328be/1436356736349/TakeVideo-Device.png )
204
130
205
-
206
- You can download the source code of
207
- the ** IOS8SwiftTakeVideoPlayerTutorial** at the ioscreator repository
208
- on [Github](https :// github.com/ioscreator/ioscreator).
209
-
210
131
你可以在[ 这里] ( https://github.com/ioscreator/ioscreator ) 下载IOS8SwiftTakeVideoPlayerTutorial项目的完整代码。
211
132
0 commit comments