Skip to content

Commit d31139a

Browse files
committed
fix: register delegate in global scope on creation, clear it when dismissed
1 parent a16d2f7 commit d31139a

File tree

1 file changed

+29
-10
lines changed

1 file changed

+29
-10
lines changed

src/imagepicker.ios.ts

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,16 @@ const defaultAssetCollectionSubtypes: NSArray<any> = NSArray.arrayWithArray(<any
1919

2020
export class ImagePicker extends data_observable.Observable {
2121
_imagePickerController: QBImagePickerController;
22-
_imagePickerControllerDelegate: ImagePickerControllerDelegate;
2322
_hostView: View;
2423

2524
// lazy-load latest frame.topmost() if _hostName is not used
2625
get hostView() {
2726
return this._hostView;
2827
}
2928

30-
get hostController() {
29+
30+
31+
get hostController(): UIViewController {
3132
let vc = this.hostView ? this.hostView.viewController : UIApplication.sharedApplication.keyWindow.rootViewController;
3233
while (
3334
vc.presentedViewController
@@ -43,9 +44,9 @@ export class ImagePicker extends data_observable.Observable {
4344
super();
4445

4546
this._hostView = hostView;
46-
this._imagePickerControllerDelegate = ImagePickerControllerDelegate.new();
4747

4848
let imagePickerController = QBImagePickerController.alloc().init();
49+
4950
imagePickerController.assetCollectionSubtypes = defaultAssetCollectionSubtypes;
5051
imagePickerController.mediaType = options.mediaType ? <QBImagePickerMediaType>options.mediaType.valueOf() : QBImagePickerMediaType.Any;
5152
imagePickerController.allowsMultipleSelection = options.mode !== 'single';
@@ -76,12 +77,13 @@ export class ImagePicker extends data_observable.Observable {
7677

7778
present() {
7879
return new Promise<void>((resolve, reject) => {
79-
this._imagePickerControllerDelegate._resolve = resolve;
80-
this._imagePickerControllerDelegate._reject = reject;
80+
const imagePickerControllerDelegate = ImagePickerControllerDelegate.new();
81+
imagePickerControllerDelegate._resolve = resolve;
82+
imagePickerControllerDelegate._reject = reject;
83+
84+
this._imagePickerController.delegate = imagePickerControllerDelegate;
8185

82-
this.hostController.presentViewControllerAnimatedCompletion(this._imagePickerController, true, () => {
83-
this._imagePickerController.delegate = this._imagePickerControllerDelegate;
84-
});
86+
this.hostController.presentViewControllerAnimatedCompletion(this._imagePickerController, true, null);
8587
});
8688
}
8789
}
@@ -93,6 +95,8 @@ export class ImagePickerControllerDelegate extends NSObject implements QBImagePi
9395
qb_imagePickerControllerDidCancel?(imagePickerController: QBImagePickerController): void {
9496
imagePickerController.dismissViewControllerAnimatedCompletion(true, null);
9597
this._reject(new Error("Selection canceled."));
98+
99+
this.deRegisterFromGlobal();
96100
}
97101

98102
qb_imagePickerControllerDidFinishPickingAssets?(imagePickerController: QBImagePickerController, iosAssets: NSArray<any>): void {
@@ -112,6 +116,7 @@ export class ImagePickerControllerDelegate extends NSObject implements QBImagePi
112116
this._resolve(assets);
113117

114118
imagePickerController.dismissViewControllerAnimatedCompletion(true, () => {
119+
this.deRegisterFromGlobal();
115120
// FIX: possible memory issue when picking images many times.
116121
// Not the best solution, but the only one working for now
117122
// https://github.com/NativeScript/nativescript-imagepicker/issues/222
@@ -120,10 +125,24 @@ export class ImagePickerControllerDelegate extends NSObject implements QBImagePi
120125

121126
}
122127

123-
public static ObjCProtocols = [QBImagePickerControllerDelegate];
128+
// FIX: stores a reference to global scope, so that the delegate is not collected in native
129+
// https://github.com/NativeScript/nativescript-imagepicker/issues/251
130+
private registerToGlobal(): any {
131+
(<any>global).imagePickerControllerDelegate = this;
132+
}
133+
134+
private deRegisterFromGlobal(): any {
135+
(<any>global).imagePickerControllerDelegate = null;
136+
}
124137

138+
public static ObjCProtocols = [QBImagePickerControllerDelegate];
139+
125140
static new(): ImagePickerControllerDelegate {
126-
return <ImagePickerControllerDelegate>super.new(); // calls new() on the NSObject
141+
const instance = <ImagePickerControllerDelegate>super.new(); // calls new() on the NSObject
142+
143+
instance.registerToGlobal();
144+
145+
return instance;
127146
}
128147
}
129148

0 commit comments

Comments
 (0)