萤石云 iOSSDK,移植跨平台相关工程
JLChen
2021-02-02 9100afbe1805413504840e6957097be638579045
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
//
//  EZMultiChannelRealPlayVC.m
//  EZOpenSDKDemo
//
//  Created by yuqian on 2020/3/10.
//  Copyright © 2020 hikvision. All rights reserved.
//
 
#import "EZMultiChannelRealPlayVC.h"
#import "EZOnlineCameraCell.h"
#import "EZOfflineCameraCell.h"
#import "EZEncryptCameraCell.h"
#import "EZDeviceInfo.h"
#import "EZCameraInfo.h"
#import "UIAlertController+TextField.h"
#import "EZLivePlayViewController.h"
#import "UIView+Toast.h"
 
 
@interface EZMultiChannelRealPlayVC () <UICollectionViewDataSource, UICollectionViewDelegate>
 
@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) UICollectionViewFlowLayout * flowLayout;
 
@property (nonatomic, strong) NSArray *devices;
@property (nonatomic, strong) NSMutableArray *cameras;
@property (nonatomic, strong) NSMutableArray *playingCameras;
@property (nonatomic, assign) BOOL thumbMode;
 
@end
 
static NSString * identifier1 = @"EZOnlineCameraCell";
static NSString * identifier2 = @"EZOfflineCameraCell";
static NSString * identifier3 = @"EZEncryptCameraCell";
 
static CGFloat kMagin = 20.f;
 
@implementation EZMultiChannelRealPlayVC
 
- (instancetype)initWithDevices:(NSArray *)devices
{
    self = [super init];
    if (self) {
        [self gatherCamerasData:devices];
    }
    return self;
}
 
- (void)viewDidLoad {
    [super viewDidLoad];
 
    self.title = @"多通道预览";
    self.view.backgroundColor = [UIColor whiteColor];
    
    UIBarButtonItem *rightItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemBookmarks target:self action:@selector(resizeCollectionLayout)];
    self.navigationItem.rightBarButtonItem = rightItem;
    
    [self.view addSubview:self.collectionView];
}
 
- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
 
    [self schedulePlayCameras];
}
 
- (void)viewDidDisappear:(BOOL)animated {
    [super viewDidDisappear:animated];
 
    [self stopAllCameraPreview];
}
 
-(UICollectionView *)collectionView{
 
    if (!_collectionView) {
 
        self.flowLayout = [[UICollectionViewFlowLayout alloc]init];
        CGFloat itemWidth = (self.view.frame.size.width - 2 * kMagin);
 
        _flowLayout.itemSize = CGSizeMake(itemWidth, itemWidth * 9/16);
        _flowLayout.minimumLineSpacing = 10;
        _flowLayout.minimumInteritemSpacing = 10;
        _flowLayout.sectionInset = UIEdgeInsetsMake(kMagin, kMagin, kMagin, kMagin);
 
        _collectionView = [[UICollectionView alloc]initWithFrame:self.view.frame collectionViewLayout:_flowLayout];
        _collectionView.showsVerticalScrollIndicator = NO;
        _collectionView.bounces = NO;
        _collectionView.backgroundColor = [UIColor whiteColor];
        [_collectionView registerClass:[EZOnlineCameraCell class] forCellWithReuseIdentifier:identifier1];
        [_collectionView registerClass:[EZOfflineCameraCell class] forCellWithReuseIdentifier:identifier2];
        [_collectionView registerClass:[EZEncryptCameraCell class] forCellWithReuseIdentifier:identifier3];
        _collectionView.dataSource = self;
        _collectionView.delegate = self;
    }
    return _collectionView;
}
 
- (NSMutableArray *)playingCameras
{
    if (!_playingCameras) {
        _playingCameras = [NSMutableArray arrayWithCapacity:5];
    }
    return _playingCameras;
}
 
#pragma mark - Fetch data
 
- (void) gatherCamerasData:(NSArray *)devices {
    
    self.devices = devices;
    self.cameras = [NSMutableArray arrayWithCapacity:5];
    
    for (EZDeviceInfo *deviceInfo in devices) {
        for (EZCameraInfo *cameraInfo in deviceInfo.cameraInfo)
        {
            cameraInfo.status = deviceInfo.status;
            cameraInfo.isEncrypt = deviceInfo.isEncrypt;
            [self.cameras addObject:cameraInfo];
        }
    }
}
 
#pragma mark - Action
 
- (void) resizeCollectionLayout
{
    
    self.thumbMode = !self.thumbMode;
       
    CGFloat itemWidth;
    if (self.thumbMode) {
        itemWidth = (self.view.frame.size.width - 3 * kMagin)/2;
        _flowLayout.itemSize = CGSizeMake(itemWidth, itemWidth * 9/16);
    }
    else {
        itemWidth = (self.view.frame.size.width - 2 * kMagin);
        _flowLayout.itemSize = CGSizeMake(itemWidth, itemWidth * 9/16);
    }
 
    [self.collectionView performBatchUpdates:^{
 
        [self.collectionView reloadData];
        
    } completion:^(BOOL finished) {
        [self schedulePlayCameras];
    }];
}
 
#pragma mark - CollectionView Delegate
 
- (NSInteger)collectionView:(nonnull UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return _cameras.count;
}
 
- (nonnull __kindof UICollectionViewCell *)collectionView:(nonnull UICollectionView *)collectionView cellForItemAtIndexPath:(nonnull NSIndexPath *)indexPath {
 
    EZCameraInfo *camera = _cameras[indexPath.row];
    NSString *verifyCode = [[GlobalKit shareKit].deviceVerifyCodeBySerial objectForKey:camera.deviceSerial];
    
    if (camera.status == 1) {
        
        if (camera.isEncrypt && verifyCode.length == 0) {
            
            EZEncryptCameraCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier3 forIndexPath:indexPath];
            return cell;
        }
        else {
            EZOnlineCameraCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier1 forIndexPath:indexPath];
            return cell;
        }
    }
    else {
        EZOfflineCameraCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier2 forIndexPath:indexPath];
        return cell;
    }
}
 
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    
    id cell = [collectionView cellForItemAtIndexPath:indexPath];
    EZCameraInfo *cameraInfo = _cameras[indexPath.row];
    
    if ([cell isKindOfClass:[EZEncryptCameraCell class]]) {
        
        __weak typeof(self) weakSelf = self;
        UIAlertController *vc = [UIAlertController showAlertVC:cameraInfo.deviceSerial confirmHandler:^(NSString * _Nonnull code) {
            
            __strong typeof(weakSelf) strongSelf = weakSelf;
            if (code.length >= 6) {
                [collectionView reloadItemsAtIndexPaths:@[indexPath]];
                dispatch_async(dispatch_get_main_queue(),^{
                    [strongSelf schedulePlayCameras];
                });
            }
            else{
                [strongSelf.view makeToastCenter:@"请输入正确的验证码"];
            }
        }];
        [self presentViewController:vc animated:YES completion:nil];
    }
    else if ([cell isKindOfClass:[EZOnlineCameraCell class]]) {
        
        EZLivePlayViewController *vc = [[UIStoryboard storyboardWithName:@"EZMain" bundle:nil] instantiateViewControllerWithIdentifier:@"EZLivePlayViewController"];
        vc.deviceInfo = [self getSelectedDeviceInfo:cameraInfo.deviceSerial];
        vc.cameraIndex = cameraInfo.cameraNo-1;
        [self.navigationController pushViewController:vc animated:YES];
    }
}
 
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
    if (!decelerate) {
        [self schedulePlayCameras];
    }
}
 
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    NSLog(@"Mulitplay ----- %s", __func__);
    
    [self schedulePlayCameras];
}
 
#pragma mark - Private Methods
 
- (EZDeviceInfo *) getSelectedDeviceInfo:(NSString *)deviceSerial {
    
    for (EZDeviceInfo *deviceInfo in _devices) {
        if ([deviceInfo.deviceSerial isEqualToString:deviceSerial]) {
            return deviceInfo;
        }
    }
    return nil;
}
 
- (void) schedulePlayCameras
{
    [self clearOutsideCamera];
    
    for (UITableViewCell *cell in self.collectionView.visibleCells)
    {
        if ([cell isMemberOfClass:[EZOnlineCameraCell class]])
        {
            EZOnlineCameraCell *onlineCameraCell = (EZOnlineCameraCell *)cell;
            NSIndexPath *indexPath = [self.collectionView indexPathForCell:onlineCameraCell];
            EZCameraInfo *camera = _cameras[indexPath.row];
            NSString *verifyCode = [[GlobalKit shareKit].deviceVerifyCodeBySerial objectForKey:camera.deviceSerial];
            
            if (!onlineCameraCell.isPlaying)
            {
                [onlineCameraCell startPlayWithDeviceSerial:camera.deviceSerial cameraNo:camera.cameraNo verifyCode:verifyCode];
            }
            
            if (![self.playingCameras containsObject:onlineCameraCell])
            {
                [self.playingCameras addObject:onlineCameraCell];
            }
        }
    }
    NSLog(@"Mulitplay ----- after schedule ------ playingCameras :%@, visibleCells :%@", self.playingCameras, self.collectionView.visibleCells);
}
 
- (void) clearOutsideCamera
{
    NSLog(@"Mulitplay ----- before clear ------ playingCameras :%@, visibleCells :%@", self.playingCameras, self.collectionView.visibleCells);
    
    NSEnumerator *enumerator = [self.playingCameras reverseObjectEnumerator];
    for (EZOnlineCameraCell *cell in enumerator)
    {
        if (![self.collectionView.visibleCells containsObject:cell])
        {
            [cell stopPlay];
 
            if ([self.playingCameras containsObject:cell])
            {
                [self.playingCameras removeObject:cell];
            }
        }
    }
    
    NSLog(@"Mulitplay ----- after clear ------ playingCameras :%@, visibleCells :%@", self.playingCameras, self.collectionView.visibleCells);
}
 
-(void) stopAllCameraPreview
{
    for (EZOnlineCameraCell *cell in self.playingCameras)
    {
        [cell stopPlay];
    }
}
 
@end