Created
April 21, 2015 19:55
-
-
Save markrickert/4b2ad79ac744a2ecae88 to your computer and use it in GitHub Desktop.
RubyMotion image capturing module
This file contains hidden or 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
# Requires BubbleWrap gem with 'bubble-wrap/camera' required. | |
module ImageCapture | |
# Usage: | |
# capture_image({}) do | |
# # @captured_image should be an instance of UIImage | |
# end | |
# | |
# Options: | |
# { | |
# allows_editing: true, | |
# save_photo: true, | |
# media_types: [:image] | |
# } | |
# | |
# Requires @strings variable to be popluated with the following keys: | |
# :take_photo | |
# :existing_photo | |
# :cancel | |
# :photo_save_error | |
def capture_image(options = {}, &block) | |
@options = { | |
allows_editing: true, | |
save_photo: true, | |
media_types: [:image] | |
}.merge(options) | |
@callback = block | |
@buttons = [] | |
# Push whatever is available | |
@buttons << @strings[:take_photo] if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceTypeCamera) | |
@buttons << @strings[:existing_photo] if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceTypePhotoLibrary) | |
#Create actionsheet | |
sheet = UIActionSheet.new | |
sheet.delegate = self | |
@buttons.each do |btn| | |
sheet.addButtonWithTitle(btn) | |
end | |
sheet.cancelButtonIndex = sheet.addButtonWithTitle(@strings[:cancel]) | |
sheet.showInView(rmq.view_controller.view) | |
end | |
# UIActionSheet Delegate method | |
def actionSheet(sheet, didDismissWithButtonIndex:button_index) | |
case @buttons[button_index] | |
when @strings[:take_photo] | |
BW::Device.camera.rear.picture(@options) do |result| | |
@captured_image_result = result | |
@captured_image = @options[:allows_editing] == true ? result[:edited_image] : result[:original_image] | |
save_photo if @options[:save_photo] | |
@callback.call | |
end | |
when @strings[:existing_photo] | |
BW::Device.camera.photo_library.picture(@options) do |result| | |
@captured_image_result = result | |
@captured_image = @options[:allows_editing] == true ? result[:edited_image] : result[:original_image] | |
@callback.call | |
end | |
end | |
end | |
# Persist photo to camera roll before we upload | |
def save_photo | |
UIImageWriteToSavedPhotosAlbum(@captured_image, self, "thisImage:hasBeenSavedInPhotoAlbumWithError:usingContextInfo:", nil) | |
end | |
def thisImage(image, hasBeenSavedInPhotoAlbumWithError:error, usingContextInfo:ctxInfo) | |
App.alert("#{@strings[:photo_save_error]}#{error.localizedDescription}") if error | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment