Skip to content

Commit 51dd85d

Browse files
committed
fix: Handle exists assets data
1 parent 605dfb4 commit 51dd85d

File tree

2 files changed

+125
-144
lines changed

2 files changed

+125
-144
lines changed

ios/MediaResponse.swift

Lines changed: 19 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -5,47 +5,44 @@
55
// Created by Donquijote on 02/04/2023.
66
//
77

8-
import TLPhotoPicker
9-
import Photos
108
import Foundation
9+
import Photos
10+
import TLPhotoPicker
1111

1212
public struct MediaResponse {
13-
1413
public enum MediaType: String {
1514
case image, video
1615
}
1716

18-
public var data: NSMutableDictionary? = nil
19-
17+
public var data: NSMutableDictionary?
2018

2119
init(filePath: String?, mime: String?, withTLAsset TLAsset: TLPHAsset, isExportThumbnail: Bool = false) {
22-
2320
let asset = TLAsset.phAsset
2421

25-
if(asset != nil){
22+
if asset != nil {
2623
var media = [
2724
"path": filePath! as String,
2825
"localIdentifier": asset?.localIdentifier ?? "" as String,
29-
"fileName":TLAsset.originalFileName!,
30-
"width": Int(asset?.pixelWidth ?? 0 ) as NSNumber,
31-
"height": Int(asset?.pixelHeight ?? 0 ) as NSNumber,
26+
"fileName": TLAsset.originalFileName!,
27+
"width": Int(asset?.pixelWidth ?? 0) as NSNumber,
28+
"height": Int(asset?.pixelHeight ?? 0) as NSNumber,
3229
"mime": mime!,
3330
"creationDate": asset?.creationDate ?? "",
3431
"type": asset?.mediaType == .video ? "video" : "image",
35-
] as [String : Any]
32+
] as [String: Any]
3633

3734
// check video type
38-
if(asset?.mediaType == .video){
39-
//get video's thumbnail
40-
if(isExportThumbnail){
35+
if asset?.mediaType == .video {
36+
// get video's thumbnail
37+
if isExportThumbnail {
4138
media["thumbnail"] = getThumbnail(from: filePath!, in: 0.1)
4239
}
43-
//get video size
40+
// get video size
4441
TLAsset.videoSize { size in
4542
media["size"] = size
4643
}
4744
media["duration"] = asset?.duration
48-
}else{
45+
} else {
4946
TLAsset.photoSize { photoSize in
5047
media["size"] = photoSize
5148
}
@@ -54,10 +51,8 @@ public struct MediaResponse {
5451
self.data = NSMutableDictionary(dictionary: media)
5552
}
5653
}
57-
5854
}
5955

60-
6156
func getImagePathFromUIImage(uiImage: UIImage, prefix: String? = "thumb") -> String {
6257
// save to temp directory
6358
let tempDirectory = FileManager.default.urls(
@@ -70,11 +65,9 @@ func getImagePathFromUIImage(uiImage: UIImage, prefix: String? = "thumb") -> Str
7065

7166
fileManager.createFile(atPath: fullPath, contents: data, attributes: nil)
7267

73-
return fullPath;
74-
68+
return fullPath
7569
}
7670

77-
7871
func getThumbnail(from moviePath: String, in seconds: Double) -> String? {
7972
let filepath = moviePath.replacingOccurrences(
8073
of: "file://",
@@ -85,22 +78,20 @@ func getThumbnail(from moviePath: String, in seconds: Double) -> String? {
8578
let generator = AVAssetImageGenerator(asset: asset)
8679
generator.appliesPreferredTrackTransform = true
8780

88-
var _: Error? = nil
81+
var _: Error?
8982
let time = CMTimeMake(value: 1, timescale: 60)
9083

91-
var imgRef: CGImage? = nil
84+
var imgRef: CGImage?
9285
do {
9386
imgRef = try generator.copyCGImage(at: time, actualTime: nil)
94-
} catch _ {
95-
}
96-
var thumbnail: UIImage? = nil
87+
} catch _ {}
88+
var thumbnail: UIImage?
9789

9890
if let imgRef = imgRef {
9991
thumbnail = UIImage(cgImage: imgRef)
10092
}
10193

10294
let fullPath = thumbnail != nil ? getImagePathFromUIImage(uiImage: thumbnail!, prefix: "thumb") : nil
10395

104-
return fullPath;
105-
96+
return fullPath
10697
}

0 commit comments

Comments
 (0)