Class: BubbleWrap::Device::Camera
- Inherits:
-
Object
- Object
- BubbleWrap::Device::Camera
- Defined in:
- motion/core/device/ios/camera.rb
Defined Under Namespace
Modules: Error
Constant Summary collapse
- MEDIA_TYPE_HASH =
{movie: KUTTypeMovie, image: KUTTypeImage}
- CAMERA_LOCATIONS =
[:front, :rear, :none]
Instance Attribute Summary collapse
-
#location ⇒ Object
The camera location; if :none, then we can’t use source_type: :camera in #picture [:front, :rear, :none].
Class Method Summary collapse
- .any ⇒ Object (also: photo_library)
- .available? ⇒ Boolean
- .front ⇒ Object
- .media_type_available?(media_type, for_source_type: source_type) ⇒ Boolean
- .rear ⇒ Object
- .source_type_available?(source_type) ⇒ Boolean
Instance Method Summary collapse
- #dismiss ⇒ Object
- #flash? ⇒ Boolean
-
#imagePickerController(picker, didFinishPickingMediaWithInfo: info) ⇒ Object
Takes the default didFinishPickingMediaWithInfo hash, transforms the keys to be nicer symbols of :this_form instead of UIImagePickerControllerThisForm, and then sends it to the callback.
-
#imagePickerControllerDidCancel(picker) ⇒ Object
UIImagePickerControllerDelegate Methods.
-
#initialize(location = :none) ⇒ Camera
constructor
A new instance of Camera.
-
#picker ⇒ Object
Short Helper Methods.
-
#picture(options = {}, presenting_controller = nil, &block) ⇒ Object
the form { source_type: :photo_library, :camera, or :saved_photos_album; default :photo_library media_types: [] containing :image and/or :movie; default [:image] allows_editing: true/false; default false animated: true/false; default true on_dismiss: lambda; default nil }.
- #popover_from(view) ⇒ Object
-
#popoverControllerDidDismissPopover(popoverController) ⇒ Object
iPad popover is dismissed.
Constructor Details
#initialize(location = :none) ⇒ Camera
Returns a new instance of Camera.
61 62 63 |
# File 'motion/core/device/ios/camera.rb', line 61 def initialize(location = :none) self.location = location end |
Instance Attribute Details
#location ⇒ Object
The camera location; if :none, then we can’t use source_type: :camera in #picture
- :front, :rear, :none
32 33 34 |
# File 'motion/core/device/ios/camera.rb', line 32 def location @location end |
Class Method Details
.any ⇒ Object Also known as: photo_library
50 51 52 |
# File 'motion/core/device/ios/camera.rb', line 50 def any @any ||= Camera.new end |
.available? ⇒ Boolean
44 45 46 |
# File 'motion/core/device/ios/camera.rb', line 44 def self.available? UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceTypeCamera) end |
.front ⇒ Object
34 35 36 37 |
# File 'motion/core/device/ios/camera.rb', line 34 def self.front return nil if not UIImagePickerController.isCameraDeviceAvailable(UIImagePickerControllerCameraDeviceFront) @front ||= Camera.new(:front) end |
.media_type_available?(media_type, for_source_type: source_type) ⇒ Boolean
226 227 228 |
# File 'motion/core/device/ios/camera.rb', line 226 def self.media_type_available?(media_type, for_source_type: source_type) UIImagePickerController.availableMediaTypesForSourceType(source_type).member? media_type end |
.rear ⇒ Object
39 40 41 42 |
# File 'motion/core/device/ios/camera.rb', line 39 def self.rear return nil if not UIImagePickerController.isCameraDeviceAvailable(UIImagePickerControllerCameraDeviceRear) @rear ||= Camera.new(:rear) end |
.source_type_available?(source_type) ⇒ Boolean
220 221 222 |
# File 'motion/core/device/ios/camera.rb', line 220 def self.source_type_available?(source_type) UIImagePickerController.isSourceTypeAvailable(source_type) end |
Instance Method Details
#dismiss ⇒ Object
210 211 212 213 214 215 216 217 |
# File 'motion/core/device/ios/camera.rb', line 210 def dismiss if [:on_dismiss] [:on_dismiss].call(self.picker) return end self.picker.dismissViewControllerAnimated([:animated], completion: [:dismiss_completed]) end |
#flash? ⇒ Boolean
72 73 74 75 |
# File 'motion/core/device/ios/camera.rb', line 72 def flash? return false if self.location == :none UIImagePickerController.isFlashAvailableForCameraDevice(camera_device) end |
#imagePickerController(picker, didFinishPickingMediaWithInfo: info) ⇒ Object
Takes the default didFinishPickingMediaWithInfo hash, transforms the keys to be nicer symbols of :this_form instead of UIImagePickerControllerThisForm, and then sends it to the callback
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 |
# File 'motion/core/device/ios/camera.rb', line 181 def imagePickerController(picker, didFinishPickingMediaWithInfo: info) callback_info = {} info.keys.each { |k| nice_key = k.gsub("UIImagePickerController", "").underscore.to_sym val = info[k] callback_info[nice_key] = val } if media_type = callback_info[:media_type] callback_info[:media_type] = media_type_to_symbol(media_type) end @callback.call(callback_info) dismiss # iPad popover? close it if @popover @popover.dismissPopoverAnimated([:animated]) @popover = nil end end |
#imagePickerControllerDidCancel(picker) ⇒ Object
UIImagePickerControllerDelegate Methods
172 173 174 175 |
# File 'motion/core/device/ios/camera.rb', line 172 def imagePickerControllerDidCancel(picker) error(Error::CANCELED) dismiss end |
#picker ⇒ Object
Short Helper Methods
205 206 207 208 |
# File 'motion/core/device/ios/camera.rb', line 205 def picker @picker_klass ||= UIImagePickerController @picker ||= @picker_klass.alloc.init end |
#picture(options = {}, presenting_controller = nil, &block) ⇒ Object
the form
source_type: :photo_library, :camera, or :saved_photos_album; default :photo_library
media_types: [] containing :image and/or :movie; default [:image]
allows_editing: true/false; default false
animated: true/false; default true
on_dismiss: lambda; default nil
Example BW::Camera.picture(source_type: :photo_library, media_types: [:image]) do |result|
image_view = UIImageView.alloc.initWithImage(result[:original_image])
end
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 |
# File 'motion/core/device/ios/camera.rb', line 99 def picture( = {}, presenting_controller = nil, &block) @callback = block @callback.weak! if @callback && BubbleWrap.use_weak_callbacks? = { allows_editing: false, animated: true, on_dismiss: false, media_types: [:image], dismiss_completed: nil, video_quality: :medium, video_maximum_duration: 600 }.merge() # If we're using Camera.any, by default use photo library if !.has_key?(:source_type) and self.location == :none [:source_type] = :photo_library # If we're using a real Camera, by default use the camera. elsif !.has_key?(:source_type) [:source_type] = :camera end source_type_readable = [:source_type] source_type = Constants.get("UIImagePickerControllerSourceType", [:source_type]) if not Camera.source_type_available?(source_type) error(Error::SOURCE_TYPE_NOT_AVAILABLE) and return end media_types = [:media_types].collect { |mt| symbol_to_media_type(mt) } if media_types.member? nil error(Error::INVALID_MEDIA_TYPE) and return end media_types.each { |media_type| if not Camera.media_type_available?(media_type, for_source_type: source_type) error(Error::MEDIA_TYPE_NOT_AVAILABLE) and return end } self.picker.delegate = self self.picker.sourceType = source_type self.picker.mediaTypes = media_types self.picker.allowsEditing = [:allows_editing] self.picker.videoQuality = Constants.get("UIImagePickerControllerQualityType", [:video_quality]) self.picker.videoMaximumDuration = [:video_maximum_duration] if source_type_readable == :camera && ![:front, :rear].member?(self.location) raise Error::INVALID_CAMERA_LOCATION, "Can't use camera location #{self.location} with source type :camera" end if source_type_readable == :camera self.picker.cameraDevice = camera_device end presenting_controller ||= App.window.rootViewController.presentedViewController # May be nil, but handles use case of container views presenting_controller ||= App.window.rootViewController # use popover for iPad (ignore on iPhone) if Device.ipad? and source_type==UIImagePickerControllerSourceTypePhotoLibrary and @popover_in_view @popover = UIPopoverController.alloc.initWithContentViewController(picker) @popover.presentPopoverFromRect(@popover_in_view.bounds, inView:@popover_in_view, permittedArrowDirections:UIPopoverArrowDirectionAny, animated:[:animated]) else presenting_controller.presentViewController(self.picker, animated:[:animated], completion: lambda {}) end end |
#popover_from(view) ⇒ Object
56 57 58 59 |
# File 'motion/core/device/ios/camera.rb', line 56 def popover_from(view) @popover_in_view = view self end |
#popoverControllerDidDismissPopover(popoverController) ⇒ Object
iPad popover is dismissed
166 167 168 |
# File 'motion/core/device/ios/camera.rb', line 166 def popoverControllerDidDismissPopover(popoverController) @popover = nil end |