Welcome!
This is the community forum for my apps Pythonista and Editorial.
For individual support questions, you can also send an email. If you have a very short question or just want to say hello — I'm @olemoritz on Twitter.
Photos Module - Start Camera from Button
-
Sorry, I don't think I could help more.
-
You could try this code, using objective c to start the camera but it is not yet finished.
The code to get the captured picture needs to use delegate methods and is not yet written.
It's a little bit too complex for me and I would need some time...
But try it, only to see if executing the script several times also generate a crash or not.import ui import console import clipboard from objc_util import * import photos SUIViewController = ObjCClass('SUIViewController') class Extracter(ui.View): def __init__(self): # Some UI Elements #(...) # Take Photo Button self.take_photo = ui.Button(flex = 'LR', title = 'Take Photo') self.take_photo.action = self.take_photo_action # Some Button Styles #(...) self.add_subview(self.take_photo) # Take Photo Action @ui.in_background def take_photo_action(self, sender): # Show camera picker = ObjCClass('UIImagePickerController').alloc().init() picker.mame = 'picker' picker.delegate = self picker.allowsEditing = False picker.sourceType = 1 # UIImagePickerControllerSourceTypeCamera super_view = sender.superview super_view_pntr = ObjCInstance(super_view) vc = SUIViewController.viewControllerForView_(super_view_pntr) vc.presentModalViewController_animated_(picker, True) # To get the captured photo, we need to work with delegates # not yet developped if __name__=='__main__': view = Extracter() view.present('sheet')
-
One step more: delegate written but I'm still lost in converting the UIImage into an image...
The photo is taken, the photo is "used" and sent to the script via the delegate function, see the console printing, ...but img not yet available for Python code, I've not yet found why.import ui import console import clipboard from objc_util import * def imagePickerController_didFinishPickingMediaWithInfo_(self,cmd,picker,info): pick = ObjCInstance(picker) # Set delegate to nil, and release its memory: pick.setDelegate_(None) ObjCInstance(self).release() # Dismiss the sheet: pick.dismissViewControllerAnimated_completion_(True, None) infos = ObjCInstance(info) print(infos) img = infos['UIImagePickerControllerEditedImage'] # UIImage #img = infos['UIImagePickerControllerOriginalImage'] print(img) #img.show() #photos.save_image(img) SUIViewController = ObjCClass('SUIViewController') MyPickerDelegate = create_objc_class('MyPickerDelegate', methods=[imagePickerController_didFinishPickingMediaWithInfo_], protocols=['UIImagePickerControllerDelegate']) class Extracter(ui.View): def __init__(self): # Take Photo Button self.take_photo = ui.Button(flex = 'LR', title = 'Take Photo') self.take_photo.action = self.take_photo_action self.add_subview(self.take_photo) # Take Photo Action #@ui.in_background @on_main_thread def take_photo_action(self, sender): # Show camera picker = ObjCClass('UIImagePickerController').alloc().init() delegate = MyPickerDelegate.alloc().init() picker.setDelegate_(delegate) picker.allowsEditing = True picker.sourceType = 1 # UIImagePickerControllerSourceTypeCamera super_view = sender.superview super_view_pntr = ObjCInstance(super_view) vc = SUIViewController.viewControllerForView_(super_view_pntr) vc.presentModalViewController_animated_(picker, True) # Protect against import if __name__ == '__main__': view = Extracter() view.present('sheet')
-
Thanks for all the help, I am glad there are people like you out there! I will see if your code works, but I assume there is a problem with Pythonista on iOS on certain devices. I've send a friend of mine the file and he was able to run it on his iPad, but on his iPhone (iPhone 6 on iOS 9.3.3) it crashed the application.
I will backup all Pythonista modules and scripts installed, remove and reinstall the application fresh, to see if something else causes the crash.
-
Ok, good luck
-
Last try
I have not been able to convert an Objective-C UIImage into a Pythonista ui.Image/PIL image, thus I did find this way through a lot of searches, not very nice but it functions and will allow you to test this "start camera - take photo - get image - display image" without using photos module, this perhaps could function on your iPhone 6 and, in this case, it could perhaps help @omz to debug the photos.capture which crashes on your device.
Anyway, I did learn a lot on objective-c, thus I didn't loose my time (I'm retired, thus I've a lot of free time) and I definitely love this app!import ui import console import clipboard from objc_util import * import ctypes from PIL import Image def imagePickerController_didFinishPickingMediaWithInfo_(self,cmd,picker,info): pick = ObjCInstance(picker) # Set delegate to nil, and release its memory: pick.setDelegate_(None) ObjCInstance(self).release() # Dismiss the sheet: pick.dismissViewControllerAnimated_completion_(True, None) # Get UIImage infos = ObjCInstance(info) print(infos) img = infos['UIImagePickerControllerEditedImage'] # UIImage # or #img = infos['UIImagePickerControllerOriginalImage'] print(img) print(img.ptr) # I can't convert UIImage into ui.Image 🤕 # Save UIImage to jpg file func = c.UIImageJPEGRepresentation func.argtypes = [ctypes.c_void_p, ctypes.c_float] func.restype = ctypes.c_void_p x = ObjCInstance(func(img.ptr, 1.0)) x.writeToFile_atomically_('test.jpg', True) # Display jpg file image = Image.open("test.jpg") image.show() SUIViewController = ObjCClass('SUIViewController') MyPickerDelegate = create_objc_class('MyPickerDelegate', methods=[imagePickerController_didFinishPickingMediaWithInfo_], protocols=['UIImagePickerControllerDelegate']) class Extracter(ui.View): def __init__(self): # Take Photo Button self.take_photo = ui.Button(flex = 'LR', title = 'Take Photo') self.take_photo.action = self.take_photo_action self.add_subview(self.take_photo) # Take Photo Action #@ui.in_background @on_main_thread def take_photo_action(self, sender): # Show camera picker = ObjCClass('UIImagePickerController').alloc().init() delegate = MyPickerDelegate.alloc().init() picker.setDelegate_(delegate) picker.allowsEditing = True picker.sourceType = 1 # UIImagePickerControllerSourceTypeCamera super_view = sender.superview super_view_pntr = ObjCInstance(super_view) vc = SUIViewController.viewControllerForView_(super_view_pntr) vc.presentModalViewController_animated_(picker, True) # Protect against import if __name__ == '__main__': view = Extracter() view.present('sheet')