Skip to content

Commit

Permalink
Add video stickers support
Browse files Browse the repository at this point in the history
  • Loading branch information
laktyushin committed Jan 28, 2022
1 parent a0ec85b commit b8951c8
Show file tree
Hide file tree
Showing 17 changed files with 161 additions and 47 deletions.
24 changes: 24 additions & 0 deletions Example/Example.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@
091DB72B2677CB21001E1909 /* s-2.png in Resources */ = {isa = PBXBuildFile; fileRef = 091DB7282677CB21001E1909 /* s-2.png */; };
091DB72C2677CB21001E1909 /* s-3.png in Resources */ = {isa = PBXBuildFile; fileRef = 091DB7292677CB21001E1909 /* s-3.png */; };
091DB72D2677CB21001E1909 /* s-1.png in Resources */ = {isa = PBXBuildFile; fileRef = 091DB72A2677CB21001E1909 /* s-1.png */; };
09523BBB279ACA9900B61675 /* v-1.webm in Resources */ = {isa = PBXBuildFile; fileRef = 09523BB8279ACA9800B61675 /* v-1.webm */; };
09523BBC279ACA9900B61675 /* v-2.webm in Resources */ = {isa = PBXBuildFile; fileRef = 09523BB9279ACA9800B61675 /* v-2.webm */; };
09523BBD279ACA9900B61675 /* v-3.webm in Resources */ = {isa = PBXBuildFile; fileRef = 09523BBA279ACA9800B61675 /* v-3.webm */; };
09523BC1279ADB5B00B61675 /* v-2-thumb.png in Resources */ = {isa = PBXBuildFile; fileRef = 09523BBE279ADB5B00B61675 /* v-2-thumb.png */; };
09523BC2279ADB5B00B61675 /* v-3-thumb.png in Resources */ = {isa = PBXBuildFile; fileRef = 09523BBF279ADB5B00B61675 /* v-3-thumb.png */; };
09523BC3279ADB5B00B61675 /* v-1-thumb.png in Resources */ = {isa = PBXBuildFile; fileRef = 09523BC0279ADB5B00B61675 /* v-1-thumb.png */; };
09B7F2C6267ABC9A00409F87 /* a-3.tgs in Resources */ = {isa = PBXBuildFile; fileRef = 09B7F2C0267ABC9900409F87 /* a-3.tgs */; };
09B7F2C7267ABC9A00409F87 /* a-1.tgs in Resources */ = {isa = PBXBuildFile; fileRef = 09B7F2C1267ABC9900409F87 /* a-1.tgs */; };
09B7F2C8267ABC9A00409F87 /* a-2-thumb.png in Resources */ = {isa = PBXBuildFile; fileRef = 09B7F2C2267ABC9900409F87 /* a-2-thumb.png */; };
Expand All @@ -29,6 +35,12 @@
091DB7282677CB21001E1909 /* s-2.png */ = {isa = PBXFileReference; explicitFileType = compiled; path = "s-2.png"; sourceTree = "<group>"; };
091DB7292677CB21001E1909 /* s-3.png */ = {isa = PBXFileReference; explicitFileType = compiled; path = "s-3.png"; sourceTree = "<group>"; };
091DB72A2677CB21001E1909 /* s-1.png */ = {isa = PBXFileReference; explicitFileType = compiled; path = "s-1.png"; sourceTree = "<group>"; };
09523BB8279ACA9800B61675 /* v-1.webm */ = {isa = PBXFileReference; lastKnownFileType = file; path = "v-1.webm"; sourceTree = "<group>"; };
09523BB9279ACA9800B61675 /* v-2.webm */ = {isa = PBXFileReference; lastKnownFileType = file; path = "v-2.webm"; sourceTree = "<group>"; };
09523BBA279ACA9800B61675 /* v-3.webm */ = {isa = PBXFileReference; lastKnownFileType = file; path = "v-3.webm"; sourceTree = "<group>"; };
09523BBE279ADB5B00B61675 /* v-2-thumb.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = "v-2-thumb.png"; sourceTree = "<group>"; };
09523BBF279ADB5B00B61675 /* v-3-thumb.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = "v-3-thumb.png"; sourceTree = "<group>"; };
09523BC0279ADB5B00B61675 /* v-1-thumb.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = "v-1-thumb.png"; sourceTree = "<group>"; };
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>"; };
09B7F2C0267ABC9900409F87 /* a-3.tgs */ = {isa = PBXFileReference; lastKnownFileType = file; path = "a-3.tgs"; sourceTree = "<group>"; };
09B7F2C1267ABC9900409F87 /* a-1.tgs */ = {isa = PBXFileReference; lastKnownFileType = file; path = "a-1.tgs"; sourceTree = "<group>"; };
Expand Down Expand Up @@ -90,6 +102,12 @@
091DB72A2677CB21001E1909 /* s-1.png */,
091DB7282677CB21001E1909 /* s-2.png */,
091DB7292677CB21001E1909 /* s-3.png */,
09523BC0279ADB5B00B61675 /* v-1-thumb.png */,
09523BB8279ACA9800B61675 /* v-1.webm */,
09523BBE279ADB5B00B61675 /* v-2-thumb.png */,
09523BB9279ACA9800B61675 /* v-2.webm */,
09523BBF279ADB5B00B61675 /* v-3-thumb.png */,
09523BBA279ACA9800B61675 /* v-3.webm */,
09DAE5142625D317005731EB /* AppDelegate.swift */,
09DAE5162625D317005731EB /* SceneDelegate.swift */,
09DAE5182625D317005731EB /* ExampleController.swift */,
Expand Down Expand Up @@ -177,13 +195,19 @@
isa = PBXResourcesBuildPhase;
buildActionMask = 2147483647;
files = (
09523BBB279ACA9900B61675 /* v-1.webm in Resources */,
091DB72C2677CB21001E1909 /* s-3.png in Resources */,
09523BC1279ADB5B00B61675 /* v-2-thumb.png in Resources */,
09523BC3279ADB5B00B61675 /* v-1-thumb.png in Resources */,
09523BBC279ACA9900B61675 /* v-2.webm in Resources */,
091DB72B2677CB21001E1909 /* s-2.png in Resources */,
09B7F2CB267ABC9A00409F87 /* a-3-thumb.png in Resources */,
09523BBD279ACA9900B61675 /* v-3.webm in Resources */,
09B7F2C7267ABC9A00409F87 /* a-1.tgs in Resources */,
09B7F2C6267ABC9A00409F87 /* a-3.tgs in Resources */,
09B7F2CA267ABC9A00409F87 /* a-2.tgs in Resources */,
09DAE5212625D318005731EB /* LaunchScreen.storyboard in Resources */,
09523BC2279ADB5B00B61675 /* v-3-thumb.png in Resources */,
091DB72D2677CB21001E1909 /* s-1.png in Resources */,
09B7F2C9267ABC9A00409F87 /* a-1-thumb.png in Resources */,
09DAE51E2625D318005731EB /* Assets.xcassets in Resources */,
Expand Down
8 changes: 4 additions & 4 deletions Example/Pods/Pods.xcodeproj/project.pbxproj

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 5 additions & 3 deletions Example/Source/Base.lproj/Main.storyboard
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<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">
<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">
<device id="retina6_0" orientation="portrait" appearance="light"/>
<dependencies>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="18093"/>
<deployment identifier="iOS"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="19519"/>
<capability name="Safe area layout guides" minToolsVersion="9.0"/>
<capability name="System colors in document resources" minToolsVersion="11.0"/>
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
Expand Down Expand Up @@ -45,10 +46,11 @@
</subviews>
</stackView>
<segmentedControl opaque="NO" contentMode="scaleToFill" misplaced="YES" contentHorizontalAlignment="left" contentVerticalAlignment="top" segmentControlStyle="plain" selectedSegmentIndex="0" translatesAutoresizingMaskIntoConstraints="NO" id="HJK-Da-aDe">
<rect key="frame" x="128" y="147" width="157" height="32"/>
<rect key="frame" x="95" y="147" width="200" height="32"/>
<segments>
<segment title="Static"/>
<segment title="Animated"/>
<segment title="Video"/>
</segments>
<connections>
<action selector="toggle:" destination="BYZ-38-t0r" eventType="valueChanged" id="p8s-6r-A4w"/>
Expand Down
25 changes: 23 additions & 2 deletions Example/Source/ExampleController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import TelegramStickersImport
class ExampleController: UIViewController {
private var staticStickerSet: StickerSet?
private var animatedStickerSet: StickerSet?
private var videoStickerSet: StickerSet?

private var selectedStickerSet: StickerSet?

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

let staticStickerSet = StickerSet(software: "TelegramStickersImport-Example", isAnimated: false)
let software = "TelegramStickersImport-Example"

let staticStickerSet = StickerSet(software: software, type: .image)
if let path = Bundle.main.path(forResource: "s-1", ofType: "png", inDirectory: nil), let data = try? Data(contentsOf: URL(fileURLWithPath: path)) {
try? staticStickerSet.addSticker(data: .image(data), emojis: ["👍", "😺"])
}
Expand All @@ -25,7 +29,7 @@ class ExampleController: UIViewController {
}
self.staticStickerSet = staticStickerSet

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

let videoStickerSet = StickerSet(software: software, type: .video)
if let path = Bundle.main.path(forResource: "v-1", ofType: "webm", inDirectory: nil), let data = try? Data(contentsOf: URL(fileURLWithPath: path)) {
try? videoStickerSet.addSticker(data: .video(data), emojis: ["😩"])
}
if let path = Bundle.main.path(forResource: "v-2", ofType: "webm", inDirectory: nil), let data = try? Data(contentsOf: URL(fileURLWithPath: path)) {
try? videoStickerSet.addSticker(data: .video(data), emojis: ["😀"])
}
if let path = Bundle.main.path(forResource: "v-3", ofType: "webm", inDirectory: nil), let data = try? Data(contentsOf: URL(fileURLWithPath: path)) {
try? videoStickerSet.addSticker(data: .video(data), emojis: ["😥"])
}
self.videoStickerSet = videoStickerSet

self.selectedStickerSet = self.staticStickerSet
}

Expand All @@ -53,6 +69,11 @@ class ExampleController: UIViewController {
self.preview1.image = UIImage(named: "a-1-thumb.png")
self.preview2.image = UIImage(named: "a-2-thumb.png")
self.preview3.image = UIImage(named: "a-3-thumb.png")
case 2:
self.selectedStickerSet = self.videoStickerSet
self.preview1.image = UIImage(named: "v-1-thumb.png")
self.preview2.image = UIImage(named: "v-2-thumb.png")
self.preview3.image = UIImage(named: "v-3-thumb.png")
default:
break
}
Expand Down
Binary file added Example/Source/v-1-thumb.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Example/Source/v-1.webm
Binary file not shown.
Binary file added Example/Source/v-2-thumb.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Example/Source/v-2.webm
Binary file not shown.
Binary file added Example/Source/v-3-thumb.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Example/Source/v-3.webm
Binary file not shown.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,12 @@ To import a sticker set of a single sticker to Telegram, add the following code
```swift
import TelegramStickersImport

let stickerSet = StickerSet(software: "Example Software", isAnimated: false)
let stickerSet = StickerSet(software: "Example Software", type: .image)
let yourStickerImage = UIImage()
if let stickerData = Sticker.StickerData(image: yourStickerImage) {
try? staticStickerSet.addSticker(data: .image(pngData), emojis: ["😎"])
try? stickerSet.addSticker(data: stickerData, emojis: ["😎"])
}
stickerSet.import()
try? stickerSet.import()
```

## License
Expand Down
3 changes: 3 additions & 0 deletions Source/Errors.swift
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import Foundation

public enum StickersError: Error {
case fileIsEmpty
case fileTooBig
case invalidDimensions
case countLimitExceeded
case dataTypeMismatch
case setIsEmpty
case emojiIsEmpty
case telegramNotInstalled
}
1 change: 1 addition & 0 deletions Source/Limits.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@ struct Limits {
static let stickerDimensionsSide = 512
static let staticStickerMaxSize = 512 * 1024
static let animatedStickerMaxSize = 64 * 1024
static let videoStickerMaxSize = 256 * 1024
static let stickerSetStickerMaxCount = 120
}
21 changes: 18 additions & 3 deletions Source/Sticker.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,31 +10,46 @@ public class Sticker {

/// Animated sticker TGS data
case animation(Data)


/// Video sticker WebM VP9 data
case video(Data)

/// A Boolean value that determines whether the sticker data contains animation
var isAnimated: Bool {
switch self {
case .image:
case .image, .video:
return false
case .animation:
return true
}
}

/// A Boolean value that determines whether the sticker data is video
var isVideo: Bool {
switch self {
case .image, .animation:
return false
case .video:
return true
}
}

/// Mime-type of sticker data
var mimeType: String {
switch self {
case .image:
return "image/png"
case .animation:
return "application/x-tgsticker"
case .video:
return "video/webm"
}
}

/// Sticker data
var data: Data {
switch self {
case let .image(data), let .animation(data):
case let .image(data), let .animation(data), let .video(data):
return data
}
}
Expand Down
Loading

0 comments on commit b8951c8

Please sign in to comment.