ALAssetsLibrary
ALAssetsGroup
1
setAssetsFilter
[signals addObject:[thumbnailSignal map:^id(UIImage *image)
{
NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
dict[@"type"] = @"video";
dict[@"document"] = @false;
dict[@"asset"] = asset;
dict[@"previewImage"] = image;
dict[@"adjustments"] = adjustments;
dict[@"dimensions"] = [NSValue valueWithCGSize:dimensions];
dict[@"duration"] = @(duration);
if (adjustments.paintingData.stickers.count > 0)
dict[@"stickers"] = adjustments.paintingData.stickers;
if (timer != nil)
dict[@"timer"] = timer;
else if (groupedId != nil && !hasAnyTimers)
dict[@"groupedId"] = groupedId;
id generatedItem = descriptionGenerator(dict, caption, entities, nil);
return generatedItem;
}]];
///
else if (dict["type"] as! NSString) == "video" {
let thumbnail = dict["previewImage"] as? UIImage
var asFile = false
if let document = dict["document"] as? NSNumber, document.boolValue {
asFile = true
}
if let asset = dict["asset"] as? TGMediaAsset {
var result: [AnyHashable: Any] = [:]
result["item" as NSString] = LegacyAssetItemWrapper(item: .video(data: .asset(asset), thumbnail: thumbnail, adjustments: dict["adjustments"] as? TGVideoEditAdjustments, caption: caption, asFile: asFile, asAnimation: false, stickers: []), timer: (dict["timer"] as? NSNumber)?.intValue, groupedId: (dict["groupedId"] as? NSNumber)?.int64Value)
return result
} else if let url = (dict["url"] as? String) ?? (dict["url"] as? URL)?.absoluteString {
let dimensions = (dict["dimensions"]! as AnyObject).cgSizeValue!
let duration = (dict["duration"]! as AnyObject).doubleValue!
var result: [AnyHashable: Any] = [:]
result["item" as NSString] = LegacyAssetItemWrapper(item: .video(data: .tempFile(path: url, dimensions: dimensions, duration: duration), thumbnail: thumbnail, adjustments: dict["adjustments"] as? TGVideoEditAdjustments, caption: caption, asFile: asFile, asAnimation: false, stickers: []), timer: (dict["timer"] as? NSNumber)?.intValue, groupedId: (dict["groupedId"] as? NSNumber)?.int64Value)
return result
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
case let .video(data, thumbnail, adjustments, caption, asFile, asAnimation, stickers):
var finalDimensions: CGSize
var finalDuration: Double
switch data {
case let .asset(asset):
finalDimensions = asset.dimensions
finalDuration = asset.videoDuration
case let .tempFile(_, dimensions, duration):
finalDimensions = dimensions
finalDuration = duration
}
if !asAnimation {
finalDimensions = TGFitSize(finalDimensions, CGSize(width: 848.0, height: 848.0))
}
var previewRepresentations: [UChatMediaImageRepresentation] = []
if let thumbnail = thumbnail {
let resource = LocalFileMediaResource(fileId: arc4random64())
let thumbnailSize = finalDimensions.aspectFitted(CGSize(width: 320.0, height: 320.0))
let thumbnailImage = TGScaleImageToPixelSize(thumbnail, thumbnailSize)!
if let thumbnailData = thumbnailImage.jpegData(compressionQuality: 0.4) {
account.postbox.mediaBox.storeResourceData(resource.id, data: thumbnailData)
previewRepresentations.append(UChatMediaImageRepresentation(dimensions: thumbnailSize, resource: resource))
}
}
if !asAnimation {
finalDimensions = TGMediaVideoConverter.dimensions(for: finalDimensions, adjustments: adjustments, preset: TGMediaVideoConversionPresetCompressedMedium)
}
var resourceAdjustments: VideoMediaResourceAdjustments?
if let adjustments = adjustments {
if adjustments.trimApplied() {
finalDuration = adjustments.trimEndValue - adjustments.trimStartValue
}
let adjustmentsData = MemoryBuffer(data: NSKeyedArchiver.archivedData(withRootObject: adjustments.dictionary()))
let digest = MemoryBuffer(data: adjustmentsData.md5Digest())
resourceAdjustments = VideoMediaResourceAdjustments(data: adjustmentsData, digest: digest)
}
let resource: UChatMediaResource
var fileName: String = "video.mp4"
switch data {
case let .asset(asset):
if let assetFileName = asset.fileName, !assetFileName.isEmpty {
fileName = (assetFileName as NSString).lastPathComponent
}
resource = VideoLibraryMediaResource(localIdentifier: asset.backingAsset.localIdentifier, conversion: asFile ? .passthrough : .compress(resourceAdjustments))
case let .tempFile(path, _, _):
if asFile || asAnimation {
if let size = fileSize(path) {
resource = LocalFileMediaResource(fileId: arc4random64(), size: size)
account.postbox.mediaBox.moveResourceData(resource.id, fromTempPath: path)
} else {
continue outer
}
} else {
resource = LocalFileVideoMediaResource(randomId: arc4random64(), path: path, adjustments: resourceAdjustments)
}
}
var fileAttributes: [UChatMediaFileAttribute] = []
fileAttributes.append(.FileName(fileName: fileName))
if asAnimation {
fileAttributes.append(.Animated)
}
/*
if !asFile {
fileAttributes.append(.Video(duration: Int(finalDuration), size: finalDimensions, flags: [.supportsStreaming]))
if let adjustments = adjustments {
if adjustments.sendAsGif {
fileAttributes.append(.Animated)
}
}
}
*/
//gxg--> 视频以文件的方式发送也带上时间和大小
fileAttributes.append(.Video(duration: Int(finalDuration), size: finalDimensions, flags: [.supportsStreaming]))
if let adjustments = adjustments {
if adjustments.sendAsGif {
fileAttributes.append(.Animated)
}
}
//<--
let media = UChatMediaFile(fileId: MediaId(namespace: Namespaces.Media.LocalFile, id: arc4random64()), partialReference: nil, resource: resource, previewRepresentations: previewRepresentations, immediateThumbnailData: nil, mimeType: "video/mp4", size: nil, attributes: fileAttributes)
var attributes: [MessageAttribute] = []
if let timer = item.timer, timer > 0 && timer <= 60 {
attributes.append(AutoremoveTimeoutMessageAttribute(timeout: Int32(timer), countdownBeginTime: nil))
}
messages.append(.message(text: caption ?? "", attributes: attributes, mediaReference: .standalone(media: media), replyToMessageId: nil, localGroupingKey: item.groupedId))
1
{\"result_code\":0,\"result_data\":[{\"forum_id\":1639118221,\"issuer_id\":175,\"issued_at\":1639118221,\"reply_cid\":1639108114,\"reply_uid\":175,\"reply_cnt\":1,\"supports\":0,\"contents\":[{\"type\":0,\"text\":\"这首好听\",\"user\":0}],\"is_support\":false}],\"result_desc\":\"OK\"}
1
ylyle.09clubs.com: 22009
1
{\"forum_id\":1639120380,\"issuer_id\":102001,\"issued_at\":1639120380,\"reply_cid\":1639049558,\"reply_uid\":122001,\"reply_cnt\":0,\"supports\":0,\"contents\":[{\"type\":0,\"text\":\"nomnomnommo mo\",\"user\":0}],\"is_support\":false}
1
{\"forum_id\":1639120499,\"issuer_id\":102001,\"issued_at\":1639120499,\"reply_cid\":1639120362,\"reply_uid\":102001,\"reply_cnt\":0,\"supports\":0,\"contents\":[{\"type\":0,\"text\":\"这些是\",\"user\":0}],\"is_support\":false}