Encrypted Images

Last Updated on : 2023-09-19 03:00:54download

[Detection alerts](https://developer.tuya.com/en/docs/app-development/securityalarm?id=Ka5vox8umv6ez) 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 `ThingEncryptImage` 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+ThingAESImage.h`.

### Display an encrypted image

**API description**

```
- (void)thing_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**

```objc
- (void)thing_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**

```objc
- (void)thing_setAESImageWithPath:(NSString *)imagePath
                    encryptKey:(NSString *)encryptKey
                     completed:(nullable ThingEncryptWebImageCompletionBlock)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 `ThingSmartCameraMessageModel.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.

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

**Example**

```objc
#import <ThingSmartCameraKit/ThingSmartCameraKit.h>
#import <ThingEncryptImage/ThingEncryptImage.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"];
    }
    ThingSmartCameraMessageModel *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 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;
}
```

## Cloud storage events

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

**Example**

```objc
#import <ThingSmartCameraKit/ThingSmartCameraKit.h>
#import <SDWebImage/UIImageView+WebCache.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;
}
```