加密图片

更新时间:2024-04-26 08:49:27下载pdf

智能设备在触发 侦测报警 时,通常会上传一张实时视频的截图到云端,通过涂鸦 IPC SDK 获取到的告警消息或者云存储事件中,都会携带一张加密后的视频截图。针对这些加密的图片,您需要使用 ThingEncryptImage 组件才能正常展示。

解密组件

通过 UIImageView 的分类来添加显示加密图片的接口。有关接口定义详情,您可以参考 UIImageView+ThingAESImage.h

显示加密图片

接口说明

- (void)thing_setAESImageWithPath:(NSString *)imagePath
                    encryptKey:(NSString *)encryptKey;

参数说明

参数 说明
imagePath 图片地址
encryptKey 加密密钥

显示加密图片并设置占位图

接口说明

- (void)thing_setAESImageWithPath:(NSString *)imagePath
                    encryptKey:(NSString *)encryptKey
              placeholderImage:(UIImage *)placeholderImage;

参数说明

参数 说明
placeholderImage 占位图,用于在图片加载完成之前展示

显示加密图片并添加加载完成的回调

接口说明

- (void)thing_setAESImageWithPath:(NSString *)imagePath
                    encryptKey:(NSString *)encryptKey
                     completed:(nullable ThingEncryptWebImageCompletionBlock)completedBlock;

参数说明

参数 说明
completedBlock 图片加载完成的回调函数

侦测报警消息

报警消息中,图片附件 ThingSmartCameraMessageModel.attachPic 的值由 图片地址加密密钥 两部分组成,并以 {path}@{key} 的格式拼接。展示图片时,您需要将这个字符串拆开。

如果图片附件字符串的值,没有 @{key} 的后缀,则表示是未加密的普通图片。

示例代码

#import <ThingSmartCameraKit/ThingSmartCameraKit.h>
#import <ThingEncryptImage/ThingEncryptImage.h>

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"cell"];
    }
    ThingSmartCameraMessageModel *messageModel = [self.messageModelList objectAtIndex:indexPath.row];
    NSArray *components = [messageModel.attachPic componentsSeparatedByString:@"@"];
    if (components.count != 2) {
        [cell.imageView thing_setImageWithURL:[NSURL URLWithString:messageModel.attachPic] placeholderImage:[self placeHolder]];
    }else {
        [cell.imageView thing_setAESImageWithPath:components.firstObject encryptKey:components.lastObject placeholderImage:[self placeHolder]];
    }
    cell.imageView.frame = CGRectMake(0, 0, 88, 50);
    cell.textLabel.text = messageModel.msgTitle;
    cell.detailTextLabel.text = messageModel.msgContent;
    return cell;
}

云存储事件

云存储事件中,事件截图 ThingSmartCloudTimeEventModel.snapshotUrl 的值是一个完整的图片地址,密钥使用云存储视频播放的统一密钥。

示例代码

#import <ThingSmartCameraKit/ThingSmartCameraKit.h>
#import <ThingEncryptImage/ThingEncryptImage.h>

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSDateFormatter *formatter = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        formatter = [[NSDateFormatter alloc] init];
        formatter.dateFormat = @"HH:mm:ss";
    });
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"event"];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"event"];
    }
    ThingSmartCloudTimeEventModel *eventModel = [self.eventModels objectAtIndex:indexPath.row];
    [cell.imageView thing_setAESImageWithPath:eventModel.snapshotUrl encryptKey:self.cloudManager.encryptKey placeholderImage:[self placeholder]];
    cell.textLabel.text = eventModel.describe;
    cell.detailTextLabel.text = [formatter stringFromDate:[NSDate dateWithTimeIntervalSince1970:eventModel.startTime]];
    return cell;
}

下载加密图片

通过 ThingEncryptImageDownloader 类下载加密图片。有关接口定义详情,您可以参考 ThingEncryptImageDownloader.h

初始化下载类

接口说明

+ (instancetype)sharedManager;

下载加密图片

接口说明

- (void)downloadEncryptImageWithPath:(NSString *)imagePath
                          encryptKey:(nullable NSString *)encryptKey
                           completed:(nullable ThingEncryptWebImageCompletionBlock)completedBlock;

参数说明

参数 说明
imagePath 图片地址
encryptKey 加密密钥
completedBlock 图片下载完成的回调函数

示例代码

#import <ThingEncryptImage/ThingEncryptImage.h>

- (void)downloadEncryptImageWithPath:(NSString *)imagePath encryptKey:(nullable NSString *)encryptKey {
    [[ThingEncryptImageDownloader sharedManager] downloadEncryptImageWithPath:imagePath encryptKey:encryptKey completed:^(UIImage * _Nullable image, NSURL * _Nullable url, ThingEncryptWebImageFromType from, ThingEncryptWebImageStage stage, NSError * _Nullable error) {
        if (image) {
            NSLog(@"download success");
        }else {
            NSLog(@"download error(%@)",error);
        }
    }];
}