-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b3bec84
commit db3346a
Showing
4 changed files
with
89 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
74 changes: 74 additions & 0 deletions
74
DocumentationTests/DocumentationTests/DocumentationTests/03-guides/10-custom-events.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import StreamVideo | ||
import StreamVideoSwiftUI | ||
import SwiftUI | ||
import Combine | ||
|
||
@MainActor | ||
fileprivate func content() { | ||
asyncContainer { | ||
let response = try await call.sendCustomEvent(["type": .string("draw"), "x": .number(10), "y": .number(20)]) | ||
} | ||
|
||
asyncContainer { | ||
func sendImageData(_ data: Data) async { | ||
guard | ||
let snapshot = UIImage(data: data), | ||
let resizedImage = resize(image: snapshot, to: .init(width: 30, height: 30)), | ||
let snapshotData = resizedImage.jpegData(compressionQuality: 0.8) | ||
else { | ||
return | ||
} | ||
|
||
do { | ||
try await call.sendCustomEvent([ | ||
"snapshot": .string(snapshotData.base64EncodedString()) | ||
]) | ||
} catch { | ||
log.error("Failed to send image.", error: error) | ||
} | ||
} | ||
|
||
func resize( | ||
image: UIImage, | ||
to targetSize: CGSize | ||
) -> UIImage? { | ||
guard | ||
image.size.width > targetSize.width || image.size.height > targetSize.height | ||
else { | ||
return image | ||
} | ||
|
||
let widthRatio = targetSize.width / image.size.width | ||
let heightRatio = targetSize.height / image.size.height | ||
|
||
// Determine the scale factor that preserves aspect ratio | ||
let scaleFactor = min(widthRatio, heightRatio) | ||
|
||
let scaledWidth = image.size.width * scaleFactor | ||
let scaledHeight = image.size.height * scaleFactor | ||
let targetRect = CGRect( | ||
x: (targetSize.width - scaledWidth) / 2, | ||
y: (targetSize.height - scaledHeight) / 2, | ||
width: scaledWidth, | ||
height: scaledHeight | ||
) | ||
|
||
// Create a new image context | ||
UIGraphicsBeginImageContextWithOptions(targetSize, false, 0) | ||
image.draw(in: targetRect) | ||
|
||
let newImage = UIGraphicsGetImageFromCurrentImageContext() | ||
UIGraphicsEndImageContext() | ||
|
||
return newImage | ||
} | ||
} | ||
|
||
asyncContainer { | ||
for await event in call.subscribe(for: CustomVideoEvent.self) { | ||
// read custom data | ||
let customData = event.custom | ||
// perform actions with the custom data. | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters