Skip to content

Commit 9fbf68f

Browse files
committed
integrated crop image for iOS and handle logic response
1 parent 1c84e36 commit 9fbf68f

File tree

4 files changed

+70
-17
lines changed

4 files changed

+70
-17
lines changed

example/src/App.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ export default function App() {
2929
maxVideo: 1,
3030
// singleSelectedMode: true,
3131
// isCrop: true,
32+
doneTitle: 'Xong',
3233
});
3334
// console.log(response);
3435
setImages(response);

ios/MediaResponse.swift

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public struct MediaResponse {
1515
case image, video
1616
}
1717

18-
public var data: NSDictionary? = nil
18+
public var data: NSMutableDictionary? = nil
1919

2020

2121
init(filePath: String?, mime: String?, withTLAsset TLAsset: TLPHAsset, isExportThumbnail: Bool = false) {
@@ -51,10 +51,26 @@ public struct MediaResponse {
5151
}
5252
}
5353

54-
self.data = NSDictionary(dictionary: media)
54+
self.data = NSMutableDictionary(dictionary: media)
5555
}
5656
}
5757

58+
}
59+
60+
61+
func getImagePathFromUIImage(uiImage: UIImage, prefix: String? = "thumb") -> String {
62+
// save to temp directory
63+
let tempDirectory = FileManager.default.urls(
64+
for: .cachesDirectory,
65+
in: .userDomainMask).map(\.path).last
66+
67+
let data = uiImage.jpegData(compressionQuality: 1.0)
68+
let fileManager = FileManager.default
69+
let fullPath = URL(fileURLWithPath: tempDirectory ?? "").appendingPathComponent("\(prefix ?? "thumb")-\(ProcessInfo.processInfo.globallyUniqueString).jpg").path
70+
71+
fileManager.createFile(atPath: fullPath, contents: data, attributes: nil)
72+
73+
return fullPath;
5874

5975
}
6076

@@ -78,18 +94,13 @@ func getThumbnail(from moviePath: String, in seconds: Double) -> String? {
7894
} catch _ {
7995
}
8096
var thumbnail: UIImage? = nil
97+
8198
if let imgRef = imgRef {
8299
thumbnail = UIImage(cgImage: imgRef)
83100
}
84-
// save to temp directory
85-
let tempDirectory = FileManager.default.urls(
86-
for: .cachesDirectory,
87-
in: .userDomainMask).map(\.path).last
88101

89-
let data = thumbnail?.jpegData(compressionQuality: 1.0)
90-
let fileManager = FileManager.default
91-
let fullPath = URL(fileURLWithPath: tempDirectory ?? "").appendingPathComponent("thumb-\(ProcessInfo.processInfo.globallyUniqueString).jpg").path
92-
fileManager.createFile(atPath: fullPath, contents: data, attributes: nil)
102+
let fullPath = getImagePathFromUIImage(uiImage: thumbnail!, prefix: "thumb")
103+
93104
return fullPath;
94105

95106
}

ios/MultipleImagePicker.swift

Lines changed: 47 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -208,11 +208,39 @@ class MultipleImagePicker: NSObject, TLPhotosPickerViewControllerDelegate,UINavi
208208

209209
func cropViewController(_ cropViewController: CropViewController, didCropToImage image: UIImage, withRect cropRect: CGRect, angle: Int) {
210210

211+
let TLAsset = self.selectedAssets.first;
212+
let filePath = getImagePathFromUIImage(uiImage: image, prefix: "crop")
213+
214+
TLAsset?.phAsset?.setValue(image.size.width, forKey: "pixelWidth")
215+
TLAsset?.phAsset?.setValue(image.size.height, forKey: "pixelHeight")
216+
217+
let object = MediaResponse(filePath: filePath, mime: "image/jpeg", withTLAsset: TLAsset!)
218+
219+
if(object.data != nil){
220+
object.data!["isCrop"] = true
221+
resolve([object.data])
222+
}else{
223+
resolve([])
224+
}
225+
226+
DispatchQueue.main.async {
227+
self.getTopMostViewController()?.dismiss(animated: true, completion: {
228+
self.getTopMostViewController()?.dismiss(animated: true, completion: nil)
229+
})
230+
}
231+
//
232+
// self.dismissComplete()
233+
// self.dismissComplete()
211234
}
212235

213236
func presentCropViewController(image: UIImage) {
214237
let cropViewController = CropViewController(image: image)
215238
cropViewController.delegate = self
239+
cropViewController.doneButtonTitle = MultipleImagePickerConfigure.doneTitle
240+
cropViewController.doneButtonColor = MultipleImagePickerConfigure.selectedColor
241+
242+
cropViewController.cancelButtonTitle = MultipleImagePickerConfigure.cancelTitle
243+
216244
self.getTopMostViewController()?.present(cropViewController, animated: true, completion: nil)
217245
}
218246

@@ -237,23 +265,34 @@ class MultipleImagePicker: NSObject, TLPhotosPickerViewControllerDelegate,UINavi
237265
return;
238266
}
239267

240-
// count
268+
// define count
241269
let withTLPHAssetsCount = withTLPHAssets.count;
242270
let selectedAssetsCount = self.selectedAssets.count;
243271

272+
// check logic code for isCrop
273+
let cropCondition = (options["singleSelectedMode"] as! Bool) && (options["isCrop"] as! Bool) && withTLPHAssets.first?.type == .photo
274+
244275
// check difference
245-
if(withTLPHAssetsCount == selectedAssetsCount && withTLPHAssets[withTLPHAssetsCount - 1].phAsset?.localIdentifier == self.selectedAssets[selectedAssetsCount-1].phAsset?.localIdentifier){
246-
// if diff => close
276+
if(withTLPHAssetsCount == selectedAssetsCount && withTLPHAssets[withTLPHAssetsCount - 1].phAsset?.localIdentifier == self.selectedAssets[selectedAssetsCount-1].phAsset?.localIdentifier && !cropCondition){
247277
dismissComplete()
248278
return;
249279
}
280+
250281

251-
let selections = NSMutableArray.init(array: withTLPHAssets);
252282
self.selectedAssets = withTLPHAssets
253-
//imageRequestOptions
254-
255283

256-
//add loading view
284+
if(cropCondition){
285+
let uiImage = withTLPHAssets.first?.fullResolutionImage
286+
if(uiImage != nil){
287+
presentCropViewController(image: (withTLPHAssets.first?.fullResolutionImage)!)
288+
return
289+
}
290+
}
291+
292+
293+
let selections = NSMutableArray.init(array: withTLPHAssets);
294+
295+
// add loading view
257296
let alert = UIAlertController(title: nil, message: "Please wait...", preferredStyle: .alert)
258297

259298
let loadingIndicator = UIActivityIndicatorView(frame: CGRect(x: 10, y: 5, width: 50, height: 50))
@@ -271,6 +310,7 @@ class MultipleImagePicker: NSObject, TLPhotosPickerViewControllerDelegate,UINavi
271310

272311
alert.view.addSubview(loadingIndicator)
273312

313+
// handle controller
274314
self.getTopMostViewController()?.present(alert, animated: true, completion: {
275315

276316
let group = DispatchGroup()

src/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ export const openPicker = (optionsPicker) => {
6464
return new Promise(async (resolve, reject) => {
6565
try {
6666
const response = await MultipleImagePicker.openPicker(options);
67+
console.log('response: ', response);
6768
if (response?.length) {
6869
if (isSingle) {
6970
resolve(response[0]);

0 commit comments

Comments
 (0)