Skip to content

Instantly share code, notes, and snippets.

@Dirk-Sandberg
Created December 20, 2019 18:55
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Dirk-Sandberg/3ce71868963cad69dabf9bb337d25fb2 to your computer and use it in GitHub Desktop.
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.
BoxLayout:
Button:
text: "hello"
on_release:
app.pick_image()
Button:
text: "update"
on_release:
app.update()
#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
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