Skip to content

Commit

Permalink
fix: Handle exists assets data
Browse files Browse the repository at this point in the history
  • Loading branch information
baronha committed Sep 8, 2023
1 parent 605dfb4 commit 51dd85d
Show file tree
Hide file tree
Showing 2 changed files with 125 additions and 144 deletions.
47 changes: 19 additions & 28 deletions ios/MediaResponse.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,47 +5,44 @@
// Created by Donquijote on 02/04/2023.
//

import TLPhotoPicker
import Photos
import Foundation
import Photos
import TLPhotoPicker

public struct MediaResponse {

public enum MediaType: String {
case image, video
}

public var data: NSMutableDictionary? = nil

public var data: NSMutableDictionary?

init(filePath: String?, mime: String?, withTLAsset TLAsset: TLPHAsset, isExportThumbnail: Bool = false) {

let asset = TLAsset.phAsset

if(asset != nil){
if asset != nil {
var media = [
"path": filePath! as String,
"localIdentifier": asset?.localIdentifier ?? "" as String,
"fileName":TLAsset.originalFileName!,
"width": Int(asset?.pixelWidth ?? 0 ) as NSNumber,
"height": Int(asset?.pixelHeight ?? 0 ) as NSNumber,
"fileName": TLAsset.originalFileName!,
"width": Int(asset?.pixelWidth ?? 0) as NSNumber,
"height": Int(asset?.pixelHeight ?? 0) as NSNumber,
"mime": mime!,
"creationDate": asset?.creationDate ?? "",
"type": asset?.mediaType == .video ? "video" : "image",
] as [String : Any]
] as [String: Any]

// check video type
if(asset?.mediaType == .video){
//get video's thumbnail
if(isExportThumbnail){
if asset?.mediaType == .video {
// get video's thumbnail
if isExportThumbnail {
media["thumbnail"] = getThumbnail(from: filePath!, in: 0.1)
}
//get video size
// get video size
TLAsset.videoSize { size in
media["size"] = size
}
media["duration"] = asset?.duration
}else{
} else {
TLAsset.photoSize { photoSize in
media["size"] = photoSize
}
Expand All @@ -54,10 +51,8 @@ public struct MediaResponse {
self.data = NSMutableDictionary(dictionary: media)
}
}

}


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

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

return fullPath;

return fullPath
}


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

var _: Error? = nil
var _: Error?
let time = CMTimeMake(value: 1, timescale: 60)

var imgRef: CGImage? = nil
var imgRef: CGImage?
do {
imgRef = try generator.copyCGImage(at: time, actualTime: nil)
} catch _ {
}
var thumbnail: UIImage? = nil
} catch _ {}
var thumbnail: UIImage?

if let imgRef = imgRef {
thumbnail = UIImage(cgImage: imgRef)
}

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

return fullPath;

return fullPath
}
Loading

0 comments on commit 51dd85d

Please sign in to comment.