有时我们需要在界面上显示视频的缩略图,这样用户不用点开也能大概了解到视频的内容。下面分别演示如何获取本地视频,以及网络在线视频的视频截图。
样例的效果图如下,将获取到的截图(视频开始部分)显示在
imageView 中。
1,获取本地视频截图
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
|
import
UIKit
import
AVFoundation
import
MobileCoreServices
class
ViewController
:
UIViewController
{
@IBOutlet
weak
var
imageView:
UIImageView
!
override
func
viewDidLoad() {
super
.viewDidLoad()
//获取本地视频
let
filePath =
NSBundle
.mainBundle().pathForResource(
"hangge"
, ofType:
"m4v"
)
let
videoURL =
NSURL
(fileURLWithPath: filePath!)
let
avAsset =
AVAsset
(
URL
: videoURL)
//生成视频截图
let
generator =
AVAssetImageGenerator
(asset: avAsset)
generator.appliesPreferredTrackTransform =
true
let
time =
CMTimeMakeWithSeconds
(0.0,600)
var
actualTime:
CMTime
=
CMTimeMake
(0,0)
let
imageRef:
CGImageRef
= try! generator.copyCGImageAtTime(time, actualTime: &actualTime)
let
frameImg =
UIImage
(
CGImage
: imageRef)
//显示截图
self
.imageView.image = frameImg
}
override
func
didReceiveMemoryWarning() {
super
.didReceiveMemoryWarning()
}
}
|
2,获取网络视频截图
由于网络请求比较耗时,所以我们把获取在线视频的相关代码写在异步线程里。
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
|
import
UIKit
import
AVFoundation
import
MobileCoreServices
class
ViewController
:
UIViewController
{
@IBOutlet
weak
var
imageView:
UIImageView
!
override
func
viewDidLoad() {
super
.viewDidLoad()
//异步获取网络视频
dispatch_async(dispatch_get_global_queue(
DISPATCH_QUEUE_PRIORITY_DEFAULT
,0), {
//获取网络视频
let
videoURL =
NSURL
(string: url)!
let
avAsset =
AVURLAsset
(
URL
: videoURL)
//生成视频截图
let
generator =
AVAssetImageGenerator
(asset: avAsset)
generator.appliesPreferredTrackTransform =
true
let
time =
CMTimeMakeWithSeconds
(0.0,600)
var
actualTime:
CMTime
=
CMTimeMake
(0,0)
let
imageRef:
CGImageRef
= try! generator.copyCGImageAtTime(time, actualTime: &actualTime)
let
frameImg =
UIImage
(
CGImage
: imageRef)
//在主线程中显示截图
dispatch_async(dispatch_get_main_queue(), {
self
.imageView.image = frameImg
})
})
}
override
func
didReceiveMemoryWarning() {
super
.didReceiveMemoryWarning()
}
}
|
原文出自: www.hangge.com 转载请保留原文链接: http://www.hangge.com/blog/cache/detail_1194.html