网上有很多自定义相机的例子,这里只是我临时写的一个iOS自定义相机(仿微信)拍照、视频录制demo,仅供参考:
用到了下面几个库:
?
|
1
2
|
#import <AVFoundation/AVFoundation.h>
#import <AssetsLibrary/AssetsLibrary.h>
|
在使用的时候需要在Info.plist中把相关权限写进去:
?
|
1
2
3
|
Privacy - Microphone Usage Description
Privacy - Photo Library Usage Description
Privacy - Camera Usage Description
|
我在写这个demo时,是按照微信的样式写的,同样是点击拍照、长按录制视频,视频录制完直接进行播放,这里封装了一个简易的播放器:
m文件
?
|
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
|
#import "HAVPlayer.h"
#import <AVFoundation/AVFoundation.h>
@interface HAVPlayer ()
@property (nonatomic,strong) AVPlayer *player;//播放器对象
@end
@implementation HAVPlayer
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
// Drawing code
}
*/
- (instancetype)initWithFrame:(CGRect)frame withShowInView:(UIView *)bgView url:(NSURL *)url {
if (self = [self initWithFrame:frame]) {
//创建播放器层
AVPlayerLayer *playerLayer = [AVPlayerLayer playerLayerWithPlayer:self.player];
playerLayer.frame = self.bounds;
[self.layer addSublayer:playerLayer];
if (url) {
self.videoUrl = url;
}
[bgView addSubview:self];
}
return self;
}
- (void)dealloc {
[self removeAvPlayerNtf];
[self stopPlayer];
self.player = nil;
}
- (AVPlayer *)player {
if (!_player) {
_player = [AVPlayer playerWithPlayerItem:[self getAVPlayerItem]];
[self addAVPlayerNtf:_player.currentItem];
}
return _player;
}
- (AVPlayerItem *)getAVPlayerItem {
AVPlayerItem *playerItem=[AVPlayerItem playerItemWithURL:self.videoUrl];
return playerItem;
}
- (void)setVideoUrl:(NSURL *)videoUrl {
_videoUrl = videoUrl;
[self removeAvPlayerNtf];
[self nextPlayer];
}
- (void)nextPlayer {
[self.player seekToTime:CMTimeMakeWithSeconds(0, _player.currentItem.duration.timescale)];
[self.player replaceCurrentItemWithPlayerItem:[self getAVPlayerItem]];
[self addAVPlayerNtf:self.player.currentItem];
if (self.player.rate == 0) {
[self.player play];
}
}
- (void) addAVPlayerNtf:(AVPlayerItem *)playerItem {
//监控状态属性
[playerItem addObserver:self forKeyPath:@"status" options:NSKeyValueObservingOptionNew context:nil];
//监控网络加载情况属性
[playerItem addObserver:self forKeyPath:@"loadedTimeRanges" options:NSKeyValueObservingOptionNew context:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playbackFinished:) name:AVPlayerItemDidPlayToEndTimeNotification object:self.player.currentItem];
}
- (void)removeAvPlayerNtf {
AVPlayerItem *playerItem = self.player.currentItem;
[playerItem removeObserver:self forKeyPath:@"status"];
[playerItem removeObserver:self forKeyPath:@"loadedTimeRanges"];
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
- (void)stopPlayer {
if (self.player.rate == 1) {
[self.player pause];//如果在播放状态就停止
}
}
/**
* 通过KVO监控播放器状态
*
* @param keyPath 监控属性
* @param object 监视器
* @param change 状态改变
* @param context 上下文
*/
-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context{
AVPlayerItem *playerItem = object;
if ([keyPath isEqualToString:@"status"]) {
AVPlayerStatus status= [[change objectForKey:@"new"] intValue];
if(status==AVPlayerStatusReadyToPlay){
NSLog(@"正在播放...,视频总长度:%.2f",CMTimeGetSeconds(playerItem.duration));
}
}else if([keyPath isEqualToString:@"loadedTimeRanges"]){
NSArray *array=playerItem.loadedTimeRanges;
CMTimeRange timeRange = [array.firstObject CMTimeRangeValue];//本次缓冲时间范围
float startSeconds = CMTimeGetSeconds(timeRange.start);
float durationSeconds = CMTimeGetSeconds(timeRange.duration);
NSTimeInterval totalBuffer = startSeconds + durationSeconds;//缓冲总长度
NSLog(@"共缓冲:%.2f",totalBuffer);
}
}
- (void)playbackFinished:(NSNotification *)ntf {
Plog(@"视频播放完成");
[self.player seekToTime:CMTimeMake(0, 1)];
[self.player play];
}
@end
|
另外微信下面的按钮长按会出现圆弧时间条:
m文件
?
|
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
|
#import "HProgressView.h"
@interface HProgressView ()
/**
* 进度值0-1.0之间
*/
@property (nonatomic,assign)CGFloat progressValue;
@property (nonatomic, assign) CGFloat currentTime;
@end
@implementation HProgressView
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
// Drawing code
CGContextRef ctx = UIGraphicsGetCurrentContext();//获取上下文
Plog(@"width = %f",self.frame.size.width);
CGPoint center = CGPointMake(self.frame.size.width/2.0, self.frame.size.width/2.0); //设置圆心位置
CGFloat radius = self.frame.size.width/2.0-5; //设置半径
CGFloat startA = - M_PI_2; //圆起点位置
CGFloat endA = -M_PI_2 + M_PI * 2 * _progressValue; //圆终点位置
UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:center radius:radius startAngle:startA endAngle:endA clockwise:YES];
CGContextSetLineWidth(ctx, 10); //设置线条宽度
[[UIColor whiteColor] setStroke]; //设置描边颜色
CGContextAddPath(ctx, path.CGPath); //把路径添加到上下文
CGContextStrokePath(ctx); //渲染
}
- (void)setTimeMax:(NSInteger)timeMax {
_timeMax = timeMax;
self.currentTime = 0;
self.progressValue = 0;
[self setNeedsDisplay];
self.hidden = NO;
[self performSelector:@selector(startProgress) withObject:nil afterDelay:0.1];
}
- (void)clearProgress {
_currentTime = _timeMax;
self.hidden = YES;
}
- (void)startProgress {
_currentTime += 0.1;
if (_timeMax > _currentTime) {
_progressValue = _currentTime/_timeMax;
Plog(@"progress = %f",_progressValue);
[self setNeedsDisplay];
[self performSelector:@selector(startProgress) withObject:nil afterDelay:0.1];
}
if (_timeMax <= _currentTime) {
[self clearProgress];
}
}
@end
|
接下来就是相机的控制器了,由于是临时写的,所以用的xib,大家不要直接使用,直接上m文件代码吧:
?
|
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
|
