Skip to content

Commit

Permalink
asdasdasdasd
Browse files Browse the repository at this point in the history
  • Loading branch information
YellowCat98 committed May 11, 2024
1 parent 7b86bb7 commit 9d590a8
Showing 1 changed file with 32 additions and 22 deletions.
54 changes: 32 additions & 22 deletions src/mainobj.mm
Original file line number Diff line number Diff line change
Expand Up @@ -25,32 +25,42 @@ void showAlert(const char *title, const char *message) {



// Custom delegate class for image picker
@interface ImagePickerDelegate : NSObject <UINavigationControllerDelegate, UIImagePickerControllerDelegate>
@end

@implementation ImagePickerDelegate

// Delegate method called when an image is picked
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<UIImagePickerControllerInfoKey, id> *)info {
NSString *mediaType = info[UIImagePickerControllerMediaType];
if ([mediaType isEqualToString:(NSString *)kUTTypeImage]) {
UIImage *selectedImage = info[UIImagePickerControllerOriginalImage];
NSURL *imageURL = info[UIImagePickerControllerImageURL];
NSString *imageName = [imageURL lastPathComponent];

showAlert("Selected Image", [imageName UTF8String]);
}

[picker dismissViewControllerAnimated:YES completion:nil];
}

// Delegate method called when the image picker is canceled
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
[picker dismissViewControllerAnimated:YES completion:nil];
}

@end

void pickImage() {
UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;

UIViewController *rootViewController = [[[[UIApplication sharedApplication] delegate] window] rootViewController];
// Create an instance of our custom delegate class
ImagePickerDelegate *imagePickerDelegate = [[ImagePickerDelegate alloc] init];
imagePicker.delegate = imagePickerDelegate;

// Define a block type for the delegate
typedef void (^ImagePickerCompletionBlock)(UIImagePickerController *picker, NSDictionary<UIImagePickerControllerInfoKey, id> *info);

// Create the block for delegate
ImagePickerCompletionBlock completionBlock = ^(UIImagePickerController *picker, NSDictionary<UIImagePickerControllerInfoKey, id> *info) {
NSString *mediaType = info[UIImagePickerControllerMediaType];
if ([mediaType isEqualToString:(NSString *)kUTTypeImage]) {
UIImage *selectedImage = info[UIImagePickerControllerOriginalImage];
NSURL *imageURL = info[UIImagePickerControllerImageURL];
NSString *imageName = [imageURL lastPathComponent];

showAlert("Selected Image", [imageName UTF8String]);
}

[picker dismissViewControllerAnimated:YES completion:nil];
};

// Assign the block to the delegate
imagePicker.delegate = (id<UINavigationControllerDelegate, UIImagePickerControllerDelegate>)completionBlock;

// Present the image picker
// Get the root view controller to present the image picker
UIViewController *rootViewController = [[[[UIApplication sharedApplication] delegate] window] rootViewController];
[rootViewController presentViewController:imagePicker animated:YES completion:nil];
}

0 comments on commit 9d590a8

Please sign in to comment.