Watermark and Screenshot
Introduction
When you need to set copyright owner logos for educational teaching courseware and other scenarios, you can use the SDK's watermark feature.
This article mainly describes how to use the SDK to implement watermark and screenshot features.
Sample Source Code Download
Please refer to Download Sample Source Code to obtain the source code.
For related source code, please check the "/ZegoExpressExample/Examples/Others/Beautify" file.
Prerequisites
Before implementing watermark and screenshot features, please ensure:
- A project has been created in the ZEGOCLOUD Console, and valid AppID and AppSign have been obtained. For details, please refer to Console - Project Information.
- ZEGO Express SDK has been integrated into the project, and basic audio/video streaming functionality has been implemented. For details, please refer to Quick Start - Integration and Quick Start - Implementation Flow.
Usage Steps
Watermark
Call ZegoWatermark to configure a watermark image URL and the watermark's size and position in the visuals.
Call the setPublishWatermark interface to set the publishing watermark.
Watermark images only support two image formats: "PNG" and "JPEG", that is, image files with three extensions: ".png", ".jpg", and ".jpeg".
The imageURL parameter in the ZegoWatermark object supports passing two path formats.
file://[Absolute path of image in Bundle]: You need to store the image in any location in the project Bundle, obtain the absolute path of the image through the pathForResource:ofType: method of NSBundle, and add the "file://" prefix.

ZegoWatermark *watermark = [[ZegoWatermark alloc] init];
NSString *imagePath = [[NSBundle mainBundle] pathForResource:@"ZegoLogo" ofType:@"png"];
watermark.imageURL = [NSString stringWithFormat:@"file://%@", imagePath];
// The watermark layout takes the upper left corner as the origin of the coordinate system, and the area cannot exceed the size of the encoding resolution setting. If empty, it means canceling the watermark
watermark.layout = CGRectMake(0, 0, 200, 200);
// Set watermark, supports dynamic modification before and after publishing stream
[self.engine setPublishWatermark:watermark isPreviewVisible:YES];asset://[Image resource name]: You need to store the image in the "Assets.xcassets" that comes with the iOS project.

ZegoWatermark *watermark = [[ZegoWatermark alloc] init];
watermark.imageURL = @"asset://ZegoLogo";
// The watermark layout takes the upper left corner as the origin of the coordinate system, and the area cannot exceed the size of the encoding resolution setting. If empty, it means canceling the watermark
watermark.layout = CGRectMake(0, 0, 200, 200);
// Set watermark, supports dynamic modification before and after publishing stream
[self.engine setPublishWatermark:watermark isPreviewVisible:YES];Screenshot
-
After publishing stream, call the takePublishStreamSnapshot interface to take a screenshot of the publishing stream visuals.
[[ZegoExpressEngine sharedEngine] takePublishStreamSnapshot:^(int errorCode, ZGImage * _Nullable image) { if (errorCode == ZegoErrorCodeCommonSuccess && image) { NSImageView *imageView = [[NSImageView alloc]initWithFrame:CGRectMake(0, 0, NSScreen.mainScreen.frame.size.width/2, NSScreen.mainScreen.frame.size.height/2)]; imageView.image = image; imageView.imageScaling = NSImageScaleAxesIndependently; } }]; -
After playing stream, call the takePlayStreamSnapshot interface to take a screenshot of the playing stream visuals.
[[ZegoExpressEngine sharedEngine] takePlayStreamSnapshot:self.streamID callback:^(int errorCode, ZGImage * _Nullable image) { if (errorCode == ZegoErrorCodeCommonSuccess && image) { NSImageView *imageView = [[NSImageView alloc]initWithFrame:CGRectMake(0, 0, NSScreen.mainScreen.frame.size.width/2, NSScreen.mainScreen.frame.size.height/2)]; imageView.image = image; imageView.imageScaling = NSImageScaleAxesIndependently; } }];
FAQ
-
How to specify the layout in ZegoWatermark?
The watermark layout cannot exceed the currently set publishing stream video encoding resolution. For setting the publishing encoding resolution, please refer to the setVideoConfig interface.
