@@ -18,6 +18,10 @@ class MultipleImagePicker: NSObject, TLPhotosPickerViewControllerDelegate,UINavi
1818 var options = NSMutableDictionary ( ) ;
1919 var videoAssets = [ PHAsset] ( )
2020 var videoCount = 0
21+ var imageRequestOptions = PHImageRequestOptions ( ) ;
22+ var videoRequestOptions = PHVideoRequestOptions . init ( )
23+
24+
2125 // controller
2226
2327
@@ -84,6 +88,15 @@ class MultipleImagePicker: NSObject, TLPhotosPickerViewControllerDelegate,UINavi
8488 }
8589 }
8690
91+ // set image / video request option.
92+ self . imageRequestOptions. deliveryMode = . fastFormat;
93+ self . imageRequestOptions. resizeMode = . fast;
94+ self . imageRequestOptions. isNetworkAccessAllowed = true
95+ self . imageRequestOptions. isSynchronous = false
96+ self . videoRequestOptions. version = PHVideoRequestOptionsVersion . current
97+ self . videoRequestOptions. deliveryMode = PHVideoRequestOptionsDeliveryMode . automatic
98+ self . videoRequestOptions. isNetworkAccessAllowed = true
99+
87100 //config options
88101 MultipleImagePickerConfigure . tapHereToChange = self . options [ " tapHereToChange " ] as! String
89102 MultipleImagePickerConfigure . numberOfColumn = self . options [ " numberOfColumn " ] as! Int ;
@@ -173,41 +186,6 @@ class MultipleImagePicker: NSObject, TLPhotosPickerViewControllerDelegate,UINavi
173186 return media
174187 }
175188
176- func getThumbnail( from moviePath: String , in seconds: Double ) -> String ? {
177- let filepath = moviePath. replacingOccurrences (
178- of: " file:// " ,
179- with: " " )
180- let vidURL = URL ( fileURLWithPath: filepath)
181-
182- let asset = AVURLAsset ( url: vidURL, options: nil )
183- let generator = AVAssetImageGenerator ( asset: asset)
184- generator. appliesPreferredTrackTransform = true
185-
186- var _: Error ? = nil
187- let time = CMTimeMake ( value: 1 , timescale: 60 )
188-
189- var imgRef : CGImage ? = nil
190- do {
191- imgRef = try generator. copyCGImage ( at: time, actualTime: nil )
192- } catch _ {
193- }
194- var thumbnail : UIImage ? = nil
195- if let imgRef = imgRef {
196- thumbnail = UIImage ( cgImage: imgRef)
197- }
198- // save to temp directory
199- let tempDirectory = FileManager . default. urls (
200- for: . cachesDirectory,
201- in: . userDomainMask) . map ( \. path) . last
202-
203- let data = thumbnail? . jpegData ( compressionQuality: 1.0 )
204- let fileManager = FileManager . default
205- let fullPath = URL ( fileURLWithPath: tempDirectory ?? " " ) . appendingPathComponent ( " thumb- \( ProcessInfo . processInfo. globallyUniqueString) .jpg " ) . path
206- fileManager. createFile ( atPath: fullPath, contents: data, attributes: nil )
207- return fullPath;
208-
209- }
210-
211189 func shouldDismissPhotoPicker( withTLPHAssets: [ TLPHAsset ] ) -> Bool {
212190 return false
213191 }
@@ -238,35 +216,45 @@ class MultipleImagePicker: NSObject, TLPhotosPickerViewControllerDelegate,UINavi
238216 self . getTopMostViewController ( ) ? . present ( cropViewController, animated: true , completion: nil )
239217 }
240218
219+ func fetchAsset( TLAsset: TLPHAsset , completion: @escaping ( MediaResponse ) -> Void ) {
220+ let index = TLAsset . selectedOrder - 1 ;
221+
222+ TLAsset . tempCopyMediaFile ( videoRequestOptions: self . videoRequestOptions, imageRequestOptions: self . imageRequestOptions, livePhotoRequestOptions: nil , exportPreset: AVAssetExportPresetHighestQuality, convertLivePhotosToJPG: true , progressBlock: { ( progress) in
223+ } , completionBlock: { ( filePath, fileType) in
224+
225+ let object = MediaResponse ( filePath: filePath. absoluteString, mime: fileType, withTLAsset: TLAsset, isExportThumbnail: self . options [ " isExportThumbnail " ] as! Bool )
226+
227+ DispatchQueue . main. async {
228+ completion ( object)
229+ }
230+ } )
231+
232+ }
233+
241234 func dismissPhotoPicker( withTLPHAssets: [ TLPHAsset ] ) {
235+
236+ // check with asset picker
242237 if ( withTLPHAssets. count == 0 ) {
243238 self . resolve ( [ ] ) ;
244239 dismissComplete ( )
245240 return ;
246241 }
247242
243+ // count
248244 let withTLPHAssetsCount = withTLPHAssets. count;
249245 let selectedAssetsCount = self . selectedAssets. count;
250246
251- //check difference
247+ // check difference
252248 if ( withTLPHAssetsCount == selectedAssetsCount && withTLPHAssets [ withTLPHAssetsCount - 1 ] . phAsset? . localIdentifier == self . selectedAssets [ selectedAssetsCount- 1 ] . phAsset? . localIdentifier) {
249+ // if diff => close
253250 dismissComplete ( )
254251 return ;
255252 }
256253
257254 let selections = NSMutableArray . init ( array: withTLPHAssets) ;
258255 self . selectedAssets = withTLPHAssets
259256 //imageRequestOptions
260- let imageRequestOptions = PHImageRequestOptions ( ) ;
261- imageRequestOptions. deliveryMode = . fastFormat;
262- imageRequestOptions. resizeMode = . fast;
263- imageRequestOptions. isNetworkAccessAllowed = true
264- imageRequestOptions. isSynchronous = false
265-
266- let videoRequestOptions = PHVideoRequestOptions . init ( )
267- videoRequestOptions. version = PHVideoRequestOptionsVersion . current
268- videoRequestOptions. deliveryMode = PHVideoRequestOptionsDeliveryMode . automatic
269- videoRequestOptions. isNetworkAccessAllowed = true
257+
270258
271259 //add loading view
272260 let alert = UIAlertController ( title: nil , message: " Please wait... " , preferredStyle: . alert)
@@ -287,40 +275,24 @@ class MultipleImagePicker: NSObject, TLPhotosPickerViewControllerDelegate,UINavi
287275 alert. view. addSubview ( loadingIndicator)
288276
289277 self . getTopMostViewController ( ) ? . present ( alert, animated: true , completion: {
278+
290279 let group = DispatchGroup ( )
280+
291281 for TLAsset in withTLPHAssets {
292282 group. enter ( )
293- let asset = TLAsset . phAsset
294- let index = TLAsset . selectedOrder - 1 ;
295-
296- TLAsset . tempCopyMediaFile ( videoRequestOptions: videoRequestOptions, imageRequestOptions: imageRequestOptions, livePhotoRequestOptions: nil , exportPreset: AVAssetExportPresetHighestQuality, convertLivePhotosToJPG: true , progressBlock: { ( progress) in
297- print ( " progress: " , progress)
298- } , completionBlock: { ( filePath, fileType) in
299- let object = NSDictionary ( dictionary: self . createAttachmentResponse (
300- filePath: filePath. absoluteString,
301- withFilename: TLAsset . originalFileName,
302- withType: fileType,
303- withAsset: asset!,
304- withTLAsset: TLAsset
305- ) !) ;
306-
307- selections [ index] = object as Any ;
283+ self . fetchAsset ( TLAsset: TLAsset) { object in
284+ // check nil object response
285+ if ( object != nil ) {
286+ selections [ index] = object as MediaResponse ;
287+ }
308288 group. leave ( ) ;
309- } )
289+ }
310290 }
311291
312292 group. notify ( queue: . main) { [ self ] in
313293 resolve ( selections) ;
314294 DispatchQueue . main. async {
315295 alert. dismiss ( animated: true , completion: {
316- if ( ( self . options [ " singleSelectedMode " ] as! Bool ) && ( self . options [ " isCrop " ] as! Bool ) ) {
317- let image = withTLPHAssets. first? . fullResolutionImage
318-
319- if ( image != nil ) {
320- self . presentCropViewController ( image: image!)
321- return ;
322- }
323- }
324296 self . dismissComplete ( )
325297 } )
326298 }
0 commit comments