Created
December 20, 2019 18:55
-
-
Save Dirk-Sandberg/3ce71868963cad69dabf9bb337d25fb2 to your computer and use it in GitHub Desktop.
How to use a native image chooser in iOS - not perfected, just proof of concept.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
BoxLayout: | |
Button: | |
text: "hello" | |
on_release: | |
app.pick_image() | |
Button: | |
text: "update" | |
on_release: | |
app.update() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import <Photos/Photos.h> | |
@interface NativeImagePicker : UIViewController | |
@property (strong, nonatomic) UIImagePickerController *imagePickerController; | |
@end | |
@implementation NativeImagePicker | |
-(id)init { | |
NSLog(@"initializing NativeImagePicker"); | |
return self; | |
} | |
- (int)displayImagePicker { | |
PHAuthorizationStatus status = [PHPhotoLibrary authorizationStatus]; | |
if(status == PHAuthorizationStatusNotDetermined) { | |
// Request photo authorization | |
[PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus status) { | |
// User code (show imagepicker) | |
}]; | |
} else if (status == PHAuthorizationStatusAuthorized) { | |
// User code | |
} else if (status == PHAuthorizationStatusRestricted) { | |
// User code | |
} else if (status == PHAuthorizationStatusDenied) { | |
// User code | |
} | |
UIImagePickerController* imagePicker = [[UIImagePickerController alloc]init]; | |
// Check if image access is authorized | |
if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) { | |
imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary; | |
// Use delegate methods to get result of photo library -- Look up UIImagePicker delegate methods | |
imagePicker.delegate = self; | |
// Must show our imagepicker above the root view | |
UIViewController *top = [UIApplication sharedApplication].keyWindow.rootViewController; | |
[top presentViewController:imagePicker animated:YES completion: nil]; | |
} | |
return 0; | |
} | |
- (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info { | |
// Save the image as "cached.png" in a folder accessible by the app (user_data_dir in python/kivy) | |
UIImage *image = info[UIImagePickerControllerOriginalImage]; | |
NSData *imageData = UIImagePNGRepresentation(image); | |
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); | |
NSString *documentsDirectory = [paths objectAtIndex:0]; | |
NSString *imagePath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.png",@"cached"]]; | |
NSLog(@"pre writing to file"); | |
if (![imageData writeToFile:imagePath atomically:NO]) | |
{ | |
NSLog(@"Failed to cache image data to disk"); | |
} | |
else | |
{ | |
NSLog(@"the cachedImagedPath is %@",imagePath); | |
} | |
[picker dismissViewControllerAnimated:YES completion:nil];//{ | |
// <#code#> | |
//}]; | |
} | |
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
sys.path.append("/".join(x for x in __file__.split("/")[:-1])) | |
from kivy.app import App | |
from kivy.utils import platform | |
if platform == 'ios': | |
from pyobjus import autoclass | |
from kivy.uix.image import Image | |
from kivy.properties import ObjectProperty, StringProperty | |
import pyrebase | |
class MainApp(App): | |
native_image_picker = ObjectProperty(None) | |
image_path = StringProperty("") | |
def on_start(self): | |
if platform == 'ios': | |
self.native_image_picker = autoclass("NativeImagePicker").alloc().init() | |
def update(self): | |
print("Updating image...") | |
folder = "/".join(x for x in self.user_data_dir.split("/")[:-1]) | |
image_path = folder + "/" + "cached.png" | |
image = Image(source=image_path, nocache=True) | |
self.root.add_widget(image) | |
def pick_image(self): | |
if platform == 'ios': | |
self.native_image_picker.displayImagePicker() | |
MainApp().run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment