Encrypted Images

Last Updated on : 2023-05-22 06:38:32download

Detection alerts can be enabled for smart devices. In most cases, a real-time video screenshot is uploaded to the cloud if a detection alert is generated. IPC SDK is designed to encrypt this image and attach it to the result of querying an alert message or cloud storage event. The component TYEncryptImage must be used to display this encrypted image.

Decryption component

Use UIImageView to add the API method for displaying the encrypted images. For more information about the API definitions, see UIImageView+TYAESImage.h.

Display an encrypted image

API description

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

Parameters

Parameter Description
imagePath The URL of the image.
encryptKey The encryption key.

Set placeholder image and display encrypted image

API description

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

Parameters

Parameter Description
placeholderImage The placeholder image that appears before the encrypted image is loaded.

Display encrypted image and set callback

API description

- (void)ty_setAESImageWithPath:(NSString *)imagePath
                    encryptKey:(NSString *)encryptKey
                     completed:(nullable TYEncryptWebImageCompletionBlock)completedBlock;

Parameters

Parameter Description
completedBlock The callback that is executed when the encrypted image is loaded.

Detection alerts

In an alert, the value of the attached image TuyaSmartCameraMessageModel.attachPic consists of the image URL and encryption key. Both sections are concatenated in the format of {path}@{key}. To display the encrypted image, this concatenated string is parsed to get the decryption data.

If the string is not suffixed with @{key}, the attached image is not encrypted.

Example

#import <TuyaSmartCameraKit/TuyaSmartCameraKit.h>
#import <TYEncryptImage/TYEncryptImage.h>
#import <SDWebImage/UIImageView+WebCache.h>

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"cell"];
    }
    TuyaSmartCameraMessageModel *messageModel = [self.messageModelList objectAtIndex:indexPath.row];
    NSArray *components = [messageModel.attachPic componentsSeparatedByString:@"@"];
    if (components.count != 2) {
        [cell.imageView sd_setImageWithURL:[NSURL URLWithString:messageModel.attachPic] placeholderImage:[self placeHolder]];
    }else {
        [cell.imageView ty_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;
}

Cloud storage events

In cloud storage events, the value of the event screenshot TuyaSmartCloudTimeEventModel.snapshotUrl is the complete URL of the image, and the key uses the unified key for cloud-stored video playback.

Example

#import <TuyaSmartCameraKit/TuyaSmartCameraKit.h>
#import <SDWebImage/UIImageView+WebCache.h>
#import <TYEncryptImage/TYEncryptImage.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"];
    }
    TuyaSmartCloudTimeEventModel *eventModel = [self.eventModels objectAtIndex:indexPath.row];
    [cell.imageView ty_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;
}