Skip to content

Commit 999b26d

Browse files
committed
feat: add prefix: file:// for ios | add loading when cropping
1 parent 3ac3cd0 commit 999b26d

File tree

2 files changed

+59
-34
lines changed

2 files changed

+59
-34
lines changed

ios/MediaResponse.swift

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -54,19 +54,26 @@ public struct MediaResponse {
5454
}
5555
}
5656

57-
func getImagePathFromUIImage(uiImage: UIImage, prefix: String? = "thumb") -> String {
57+
func getImagePathFromUIImage(uiImage: UIImage, prefix: String? = "thumb") -> String? {
5858
// save to temp directory
59-
let tempDirectory = FileManager.default.urls(
60-
for: .cachesDirectory,
61-
in: .userDomainMask).map(\.path).last
6259

63-
let data = uiImage.jpegData(compressionQuality: 1.0)
6460
let fileManager = FileManager.default
65-
let fullPath = URL(fileURLWithPath: tempDirectory ?? "").appendingPathComponent("\(prefix ?? "thumb")-\(ProcessInfo.processInfo.globallyUniqueString).jpg").path
6661

62+
guard
63+
let tempDirectory = FileManager.default.urls(
64+
for: .cachesDirectory,
65+
in: .userDomainMask).map(\.path).last
66+
else {
67+
return nil
68+
}
69+
70+
let data = uiImage.jpegData(compressionQuality: 1.0)
71+
72+
let fullPath = URL(fileURLWithPath: tempDirectory).appendingPathComponent("\(prefix ?? "thumb")-\(ProcessInfo.processInfo.globallyUniqueString).jpg").path
73+
6774
fileManager.createFile(atPath: fullPath, contents: data, attributes: nil)
6875

69-
return fullPath
76+
return "file://" + fullPath
7077
}
7178

7279
func getVideoThumbnail(from moviePath: String, in seconds: Double) -> String? {

ios/MultipleImagePicker.swift

Lines changed: 45 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -224,20 +224,7 @@ class MultipleImagePicker: NSObject, TLPhotosPickerViewControllerDelegate, UINav
224224
// add loading view
225225
let alert = UIAlertController(title: nil, message: "Please wait...", preferredStyle: .alert)
226226

227-
let loadingIndicator = UIActivityIndicatorView(frame: CGRect(x: 10, y: 5, width: 50, height: 50))
228-
229-
loadingIndicator.hidesWhenStopped = true
230-
loadingIndicator.style = UIActivityIndicatorView.Style.gray
231-
232-
if #available(iOS 13.0, *) {
233-
loadingIndicator.color = .secondaryLabel
234-
} else {
235-
loadingIndicator.color = .black
236-
}
237-
238-
loadingIndicator.startAnimating()
239-
240-
alert.view.addSubview(loadingIndicator)
227+
alert.showLoading()
241228

242229
// handle controller
243230
self.getTopMostViewController()?.present(alert, animated: true, completion: {
@@ -330,27 +317,58 @@ extension MultipleImagePicker: TLPhotosPickerLogDelegate {
330317
// CropViewControllerDelegate
331318
extension MultipleImagePicker: CropViewControllerDelegate {
332319
func cropViewController(_ cropViewController: CropViewController, didCropToImage image: UIImage, withRect cropRect: CGRect, angle: Int) {
333-
let filePath = getImagePathFromUIImage(uiImage: image, prefix: "crop")
334-
let TLAsset = self.selectedAssets.first
320+
guard
321+
let TLAsset = self.selectedAssets.first,
322+
let filePath = getImagePathFromUIImage(uiImage: image, prefix: "crop")
323+
else {
324+
self.dismissComplete()
325+
return
326+
}
335327

336328
// Dismiss twice for crop controller & picker controller
337329
DispatchQueue.main.async {
338-
self.getTopMostViewController()?.dismiss(animated: true, completion: {
339-
self.getTopMostViewController()?.dismiss(animated: true, completion: {
340-
if filePath != "", TLAsset != nil {
341-
self.fetchAsset(TLAsset: TLAsset!) { object in
330+
cropViewController.dismiss(animated: true) {
331+
let alert = UIAlertController(title: nil, message: "Please wait...", preferredStyle: .alert)
332+
333+
alert.showLoading()
334+
335+
self.getTopMostViewController()?.present(alert, animated: true) {
336+
self.fetchAsset(TLAsset: TLAsset) { object in
342337

343-
object.data!["crop"] = [
344-
"height": image.size.height,
345-
"width": image.size.width,
346-
"path": filePath,
347-
]
338+
object.data!["crop"] = [
339+
"height": image.size.height,
340+
"width": image.size.width,
341+
"path": filePath,
342+
]
348343

344+
DispatchQueue.main.async {
349345
self.resolve([object.data])
346+
alert.dismiss(animated: true) {
347+
self.getTopMostViewController()?.dismiss(animated: true)
348+
}
350349
}
351350
}
352-
})
353-
})
351+
}
352+
}
353+
}
354+
}
355+
}
356+
357+
extension UIAlertController {
358+
func showLoading() {
359+
let loadingIndicator = UIActivityIndicatorView(frame: CGRect(x: 10, y: 5, width: 50, height: 50))
360+
361+
loadingIndicator.hidesWhenStopped = true
362+
loadingIndicator.style = UIActivityIndicatorView.Style.gray
363+
364+
if #available(iOS 13.0, *) {
365+
loadingIndicator.color = .secondaryLabel
366+
} else {
367+
loadingIndicator.color = .black
354368
}
369+
370+
loadingIndicator.startAnimating()
371+
372+
self.view.addSubview(loadingIndicator)
355373
}
356374
}

0 commit comments

Comments
 (0)