Skip to content

Commit b8951c8

Browse files
committed
Add video stickers support
1 parent a0ec85b commit b8951c8

17 files changed

+161
-47
lines changed

Example/Example.xcodeproj/project.pbxproj

+24
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,12 @@
1010
091DB72B2677CB21001E1909 /* s-2.png in Resources */ = {isa = PBXBuildFile; fileRef = 091DB7282677CB21001E1909 /* s-2.png */; };
1111
091DB72C2677CB21001E1909 /* s-3.png in Resources */ = {isa = PBXBuildFile; fileRef = 091DB7292677CB21001E1909 /* s-3.png */; };
1212
091DB72D2677CB21001E1909 /* s-1.png in Resources */ = {isa = PBXBuildFile; fileRef = 091DB72A2677CB21001E1909 /* s-1.png */; };
13+
09523BBB279ACA9900B61675 /* v-1.webm in Resources */ = {isa = PBXBuildFile; fileRef = 09523BB8279ACA9800B61675 /* v-1.webm */; };
14+
09523BBC279ACA9900B61675 /* v-2.webm in Resources */ = {isa = PBXBuildFile; fileRef = 09523BB9279ACA9800B61675 /* v-2.webm */; };
15+
09523BBD279ACA9900B61675 /* v-3.webm in Resources */ = {isa = PBXBuildFile; fileRef = 09523BBA279ACA9800B61675 /* v-3.webm */; };
16+
09523BC1279ADB5B00B61675 /* v-2-thumb.png in Resources */ = {isa = PBXBuildFile; fileRef = 09523BBE279ADB5B00B61675 /* v-2-thumb.png */; };
17+
09523BC2279ADB5B00B61675 /* v-3-thumb.png in Resources */ = {isa = PBXBuildFile; fileRef = 09523BBF279ADB5B00B61675 /* v-3-thumb.png */; };
18+
09523BC3279ADB5B00B61675 /* v-1-thumb.png in Resources */ = {isa = PBXBuildFile; fileRef = 09523BC0279ADB5B00B61675 /* v-1-thumb.png */; };
1319
09B7F2C6267ABC9A00409F87 /* a-3.tgs in Resources */ = {isa = PBXBuildFile; fileRef = 09B7F2C0267ABC9900409F87 /* a-3.tgs */; };
1420
09B7F2C7267ABC9A00409F87 /* a-1.tgs in Resources */ = {isa = PBXBuildFile; fileRef = 09B7F2C1267ABC9900409F87 /* a-1.tgs */; };
1521
09B7F2C8267ABC9A00409F87 /* a-2-thumb.png in Resources */ = {isa = PBXBuildFile; fileRef = 09B7F2C2267ABC9900409F87 /* a-2-thumb.png */; };
@@ -29,6 +35,12 @@
2935
091DB7282677CB21001E1909 /* s-2.png */ = {isa = PBXFileReference; explicitFileType = compiled; path = "s-2.png"; sourceTree = "<group>"; };
3036
091DB7292677CB21001E1909 /* s-3.png */ = {isa = PBXFileReference; explicitFileType = compiled; path = "s-3.png"; sourceTree = "<group>"; };
3137
091DB72A2677CB21001E1909 /* s-1.png */ = {isa = PBXFileReference; explicitFileType = compiled; path = "s-1.png"; sourceTree = "<group>"; };
38+
09523BB8279ACA9800B61675 /* v-1.webm */ = {isa = PBXFileReference; lastKnownFileType = file; path = "v-1.webm"; sourceTree = "<group>"; };
39+
09523BB9279ACA9800B61675 /* v-2.webm */ = {isa = PBXFileReference; lastKnownFileType = file; path = "v-2.webm"; sourceTree = "<group>"; };
40+
09523BBA279ACA9800B61675 /* v-3.webm */ = {isa = PBXFileReference; lastKnownFileType = file; path = "v-3.webm"; sourceTree = "<group>"; };
41+
09523BBE279ADB5B00B61675 /* v-2-thumb.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = "v-2-thumb.png"; sourceTree = "<group>"; };
42+
09523BBF279ADB5B00B61675 /* v-3-thumb.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = "v-3-thumb.png"; sourceTree = "<group>"; };
43+
09523BC0279ADB5B00B61675 /* v-1-thumb.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = "v-1-thumb.png"; sourceTree = "<group>"; };
3244
097A714CADE6B892C51F7D27 /* Pods-Example.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-Example.debug.xcconfig"; path = "Target Support Files/Pods-Example/Pods-Example.debug.xcconfig"; sourceTree = "<group>"; };
3345
09B7F2C0267ABC9900409F87 /* a-3.tgs */ = {isa = PBXFileReference; lastKnownFileType = file; path = "a-3.tgs"; sourceTree = "<group>"; };
3446
09B7F2C1267ABC9900409F87 /* a-1.tgs */ = {isa = PBXFileReference; lastKnownFileType = file; path = "a-1.tgs"; sourceTree = "<group>"; };
@@ -90,6 +102,12 @@
90102
091DB72A2677CB21001E1909 /* s-1.png */,
91103
091DB7282677CB21001E1909 /* s-2.png */,
92104
091DB7292677CB21001E1909 /* s-3.png */,
105+
09523BC0279ADB5B00B61675 /* v-1-thumb.png */,
106+
09523BB8279ACA9800B61675 /* v-1.webm */,
107+
09523BBE279ADB5B00B61675 /* v-2-thumb.png */,
108+
09523BB9279ACA9800B61675 /* v-2.webm */,
109+
09523BBF279ADB5B00B61675 /* v-3-thumb.png */,
110+
09523BBA279ACA9800B61675 /* v-3.webm */,
93111
09DAE5142625D317005731EB /* AppDelegate.swift */,
94112
09DAE5162625D317005731EB /* SceneDelegate.swift */,
95113
09DAE5182625D317005731EB /* ExampleController.swift */,
@@ -177,13 +195,19 @@
177195
isa = PBXResourcesBuildPhase;
178196
buildActionMask = 2147483647;
179197
files = (
198+
09523BBB279ACA9900B61675 /* v-1.webm in Resources */,
180199
091DB72C2677CB21001E1909 /* s-3.png in Resources */,
200+
09523BC1279ADB5B00B61675 /* v-2-thumb.png in Resources */,
201+
09523BC3279ADB5B00B61675 /* v-1-thumb.png in Resources */,
202+
09523BBC279ACA9900B61675 /* v-2.webm in Resources */,
181203
091DB72B2677CB21001E1909 /* s-2.png in Resources */,
182204
09B7F2CB267ABC9A00409F87 /* a-3-thumb.png in Resources */,
205+
09523BBD279ACA9900B61675 /* v-3.webm in Resources */,
183206
09B7F2C7267ABC9A00409F87 /* a-1.tgs in Resources */,
184207
09B7F2C6267ABC9A00409F87 /* a-3.tgs in Resources */,
185208
09B7F2CA267ABC9A00409F87 /* a-2.tgs in Resources */,
186209
09DAE5212625D318005731EB /* LaunchScreen.storyboard in Resources */,
210+
09523BC2279ADB5B00B61675 /* v-3-thumb.png in Resources */,
187211
091DB72D2677CB21001E1909 /* s-1.png in Resources */,
188212
09B7F2C9267ABC9A00409F87 /* a-1-thumb.png in Resources */,
189213
09DAE51E2625D318005731EB /* Assets.xcassets in Resources */,

Example/Pods/Pods.xcodeproj/project.pbxproj

+4-4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Example/Source/Base.lproj/Main.storyboard

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="18122" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" useSafeAreas="YES" colorMatched="YES" initialViewController="BYZ-38-t0r">
2+
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="19529" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" useSafeAreas="YES" colorMatched="YES" initialViewController="BYZ-38-t0r">
33
<device id="retina6_0" orientation="portrait" appearance="light"/>
44
<dependencies>
5-
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="18093"/>
5+
<deployment identifier="iOS"/>
6+
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="19519"/>
67
<capability name="Safe area layout guides" minToolsVersion="9.0"/>
78
<capability name="System colors in document resources" minToolsVersion="11.0"/>
89
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
@@ -45,10 +46,11 @@
4546
</subviews>
4647
</stackView>
4748
<segmentedControl opaque="NO" contentMode="scaleToFill" misplaced="YES" contentHorizontalAlignment="left" contentVerticalAlignment="top" segmentControlStyle="plain" selectedSegmentIndex="0" translatesAutoresizingMaskIntoConstraints="NO" id="HJK-Da-aDe">
48-
<rect key="frame" x="128" y="147" width="157" height="32"/>
49+
<rect key="frame" x="95" y="147" width="200" height="32"/>
4950
<segments>
5051
<segment title="Static"/>
5152
<segment title="Animated"/>
53+
<segment title="Video"/>
5254
</segments>
5355
<connections>
5456
<action selector="toggle:" destination="BYZ-38-t0r" eventType="valueChanged" id="p8s-6r-A4w"/>

Example/Source/ExampleController.swift

+23-2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import TelegramStickersImport
44
class ExampleController: UIViewController {
55
private var staticStickerSet: StickerSet?
66
private var animatedStickerSet: StickerSet?
7+
private var videoStickerSet: StickerSet?
8+
79
private var selectedStickerSet: StickerSet?
810

911
@IBOutlet weak var preview1: UIImageView!
@@ -13,7 +15,9 @@ class ExampleController: UIViewController {
1315
override func viewDidLoad() {
1416
super.viewDidLoad()
1517

16-
let staticStickerSet = StickerSet(software: "TelegramStickersImport-Example", isAnimated: false)
18+
let software = "TelegramStickersImport-Example"
19+
20+
let staticStickerSet = StickerSet(software: software, type: .image)
1721
if let path = Bundle.main.path(forResource: "s-1", ofType: "png", inDirectory: nil), let data = try? Data(contentsOf: URL(fileURLWithPath: path)) {
1822
try? staticStickerSet.addSticker(data: .image(data), emojis: ["👍", "😺"])
1923
}
@@ -25,7 +29,7 @@ class ExampleController: UIViewController {
2529
}
2630
self.staticStickerSet = staticStickerSet
2731

28-
let animatedStickerSet = StickerSet(software: "TGStickersImport-Example", isAnimated: true)
32+
let animatedStickerSet = StickerSet(software: software, type: .animation)
2933
if let path = Bundle.main.path(forResource: "a-1", ofType: "tgs", inDirectory: nil), let data = try? Data(contentsOf: URL(fileURLWithPath: path)) {
3034
try? animatedStickerSet.addSticker(data: .animation(data), emojis: ["😭"])
3135
}
@@ -37,6 +41,18 @@ class ExampleController: UIViewController {
3741
}
3842
self.animatedStickerSet = animatedStickerSet
3943

44+
let videoStickerSet = StickerSet(software: software, type: .video)
45+
if let path = Bundle.main.path(forResource: "v-1", ofType: "webm", inDirectory: nil), let data = try? Data(contentsOf: URL(fileURLWithPath: path)) {
46+
try? videoStickerSet.addSticker(data: .video(data), emojis: ["😩"])
47+
}
48+
if let path = Bundle.main.path(forResource: "v-2", ofType: "webm", inDirectory: nil), let data = try? Data(contentsOf: URL(fileURLWithPath: path)) {
49+
try? videoStickerSet.addSticker(data: .video(data), emojis: ["😀"])
50+
}
51+
if let path = Bundle.main.path(forResource: "v-3", ofType: "webm", inDirectory: nil), let data = try? Data(contentsOf: URL(fileURLWithPath: path)) {
52+
try? videoStickerSet.addSticker(data: .video(data), emojis: ["😥"])
53+
}
54+
self.videoStickerSet = videoStickerSet
55+
4056
self.selectedStickerSet = self.staticStickerSet
4157
}
4258

@@ -53,6 +69,11 @@ class ExampleController: UIViewController {
5369
self.preview1.image = UIImage(named: "a-1-thumb.png")
5470
self.preview2.image = UIImage(named: "a-2-thumb.png")
5571
self.preview3.image = UIImage(named: "a-3-thumb.png")
72+
case 2:
73+
self.selectedStickerSet = self.videoStickerSet
74+
self.preview1.image = UIImage(named: "v-1-thumb.png")
75+
self.preview2.image = UIImage(named: "v-2-thumb.png")
76+
self.preview3.image = UIImage(named: "v-3-thumb.png")
5677
default:
5778
break
5879
}

Example/Source/v-1-thumb.png

218 KB
Loading

Example/Source/v-1.webm

87.6 KB
Binary file not shown.

Example/Source/v-2-thumb.png

233 KB
Loading

Example/Source/v-2.webm

114 KB
Binary file not shown.

Example/Source/v-3-thumb.png

221 KB
Loading

Example/Source/v-3.webm

97.2 KB
Binary file not shown.

README.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,12 @@ To import a sticker set of a single sticker to Telegram, add the following code
4242
```swift
4343
import TelegramStickersImport
4444

45-
let stickerSet = StickerSet(software: "Example Software", isAnimated: false)
45+
let stickerSet = StickerSet(software: "Example Software", type: .image)
4646
let yourStickerImage = UIImage()
4747
if let stickerData = Sticker.StickerData(image: yourStickerImage) {
48-
try? staticStickerSet.addSticker(data: .image(pngData), emojis: ["😎"])
48+
try? stickerSet.addSticker(data: stickerData, emojis: ["😎"])
4949
}
50-
stickerSet.import()
50+
try? stickerSet.import()
5151
```
5252

5353
## License

Source/Errors.swift

+3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
import Foundation
22

33
public enum StickersError: Error {
4+
case fileIsEmpty
45
case fileTooBig
56
case invalidDimensions
67
case countLimitExceeded
78
case dataTypeMismatch
89
case setIsEmpty
10+
case emojiIsEmpty
11+
case telegramNotInstalled
912
}

Source/Limits.swift

+1
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,6 @@ struct Limits {
22
static let stickerDimensionsSide = 512
33
static let staticStickerMaxSize = 512 * 1024
44
static let animatedStickerMaxSize = 64 * 1024
5+
static let videoStickerMaxSize = 256 * 1024
56
static let stickerSetStickerMaxCount = 120
67
}

Source/Sticker.swift

+18-3
Original file line numberDiff line numberDiff line change
@@ -10,31 +10,46 @@ public class Sticker {
1010

1111
/// Animated sticker TGS data
1212
case animation(Data)
13-
13+
14+
/// Video sticker WebM VP9 data
15+
case video(Data)
16+
1417
/// A Boolean value that determines whether the sticker data contains animation
1518
var isAnimated: Bool {
1619
switch self {
17-
case .image:
20+
case .image, .video:
1821
return false
1922
case .animation:
2023
return true
2124
}
2225
}
2326

27+
/// A Boolean value that determines whether the sticker data is video
28+
var isVideo: Bool {
29+
switch self {
30+
case .image, .animation:
31+
return false
32+
case .video:
33+
return true
34+
}
35+
}
36+
2437
/// Mime-type of sticker data
2538
var mimeType: String {
2639
switch self {
2740
case .image:
2841
return "image/png"
2942
case .animation:
3043
return "application/x-tgsticker"
44+
case .video:
45+
return "video/webm"
3146
}
3247
}
3348

3449
/// Sticker data
3550
var data: Data {
3651
switch self {
37-
case let .image(data), let .animation(data):
52+
case let .image(data), let .animation(data), let .video(data):
3853
return data
3954
}
4055
}

0 commit comments

Comments
 (0)