Chapter 6. Device Capability – Power Unleashed

"Software will give you respect, but hardware will give you the Power."

- Akshat Paul

An iPhone is not only used for making calls, surfing the Internet, and playing music, but it is also the most advanced piece of hardware that can be used to take pictures, know your present location, comprehend gestures, and to do so many other things. So why not take advantage of these incredible device capabilities in your application. The beauty of these features is that just by tapping into the tools that the iPhone SDK provides, one can quickly import pictures, locations, and maps with minimal lines of code.

In this chapter we will focus on the following topics:

  • Camera
  • Location Manager (GPS)
  • Gestures
  • Core Data
  • Address Book

Camera – smile please!

The camera is probably the most widely used feature of an iOS device. In this section, we will cover the most frequently used Camera events by creating an application that will allow us to take a picture using the Camera device and to select a picture from the Gallery.

An iPhone implements image selection through a picker that allows us to get images from different sources, such as Camera Roll or Photo Library. The UIImagePickerController class provides basic, customizable user interfaces (UIs) for taking pictures and videos, also providing the user with some simple editing capabilities for newly captured media.

The role and appearance of a UIImagePickerController class depends on the value of sourceType assigned to it. There are three ways to choose the source of an image, as follows:

  • Choose from Camera:
    imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
    
  • Choose from any folder in Gallery:
    imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    
  • Choose from Photo Album (Camera Roll):
    imagePicker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
    

Camera example

Let's create an application that will allow us to capture a photo from the camera and select an image from Photo Gallery. Perform the following steps:

  1. Create an application with the motion command:
    motion create CameraExample
    
  2. Update app_delegate.rb and set the root controller to CameraController:
    class AppDelegate
      def application(application, didFinishLaunchingWithOptions:launchOptions)
         @window = UIWindow.alloc.initWithFrame(UIScreen.mainScreen.bounds)
         @window.rootViewController = CameraController.alloc.init
         @window.makeKeyAndVisible
         true
      end
    end
  3. Create a file named camera_controller.rb inside the app folder:
    class CameraController < UIViewController
    
      def viewDidLoad
        view.backgroundColor = UIColor.underPageBackgroundColor
        load_view
      end
    
      def load_view
        @camera_button = UIButton.buttonWithType(UIButtonTypeRoundedRect)
        @camera_button.frame  = [[50, 20], [200, 50]]
        @camera_button.setTitle("Click from camera", forState:UIControlStateNormal)
        @camera_button.addTarget(self, action: :start_camera, forControlEvents:UIControlEventTouchUpInside)
        view.addSubview(@camera_button)
    
        @gallery_button = UIButton.buttonWithType(UIButtonTypeRoundedRect)
        @gallery_button.frame  = [[50, 100], [200, 50]]
        @gallery_button.setTitle("Chose from Gallery", forState:UIControlStateNormal)
        @gallery_button.addTarget(self, action: :open_gallery, forControlEvents:UIControlEventTouchUpInside)
        view.addSubview(@gallery_button)
    
        @image_picker = UIImagePickerController.alloc.init
        @image_picker.delegate = self 
      end
    
      def imagePickerController(picker, didFinishPickingImage:image, editingInfo:info)
        self.dismissModalViewControllerAnimated(true)
        @image_view.removeFromSuperview if @image_view
        @image_view = UIImageView.alloc.initWithImage(image)
        @image_view.frame = [[50, 200], [200, 180]]
        view.addSubview(@image_view)
      end
    
      def start_camera
        if camera_present?
          @image_picker.sourceType = UIImagePickerControllerSourceTypeCamera
          presentModalViewController(@image_picker, animated:true)
        else
          show_alert
        end
      end
      def open_gallery
        @image_picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary
        presentModalViewController(@image_picker, animated:true)
      end
      def show_alert
        alert = UIAlertView.new  
        alert.message = ‘No Camera in device'
        alert.show
      end
      def camera_present?
        UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceTypeCamera)
      end
    end

Let's see what we have done so far by testing our application on the simulator using the following command:

$rake

We can see the results as shown in the following screenshot:

Camera example

As we are using an iPhone simulator, we cannot access the camera hardware. However, if we test our application with an iPhone device, we will be able to use the camera hardware and capture images from it. Now let's choose an image from Gallery by clicking on the Choose from Gallery button:

Camera example

Understanding the Camera code

First, we need to initiate two buttons for the photo-taking process and choose a picture from Gallery. We will also create an image picker:

  def load_view
    @camera_button = UIButton.buttonWithType(UIButtonTypeRoundedRect)
    @camera_button.frame  = [[50, 20], [200, 50]]
    @camera_button.setTitle("Click from camera", forState:UIControlStateNormal)
    @camera_button.addTarget(self, action: :start_camera, forControlEvents:UIControlEventTouchUpInside)
    view.addSubview(@camera_button)
    @gallery_button = UIButton.buttonWithType(UIButtonTypeRoundedRect)
    @gallery_button.frame  = [[50, 100], [200, 50]]
    @gallery_button.setTitle("Choose from Gallery", forState:UIControlStateNormal)
    @gallery_button.addTarget(self, action: :open_gallery, forControlEvents:UIControlEventTouchUpInside)
    view.addSubview(@gallery_button)

    @image_picker = UIImagePickerController.alloc.init
    @image_picker.delegate = self 
  end

So when we click on the Click from camera and Choose from Gallery buttons, the start_camera and open_gallery actions will be called, respectively:

  def start_camera
    if camera_present?
      @image_picker.sourceType = UIImagePickerControllerSourceTypeCamera
      presentModalViewController(@image_picker, animated:true)
    else
      show_alert
    end
  end

  def open_gallery
    @image_picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary
    presentModalViewController(@image_picker, animated:true)
  end
  def show_alert
    alert = UIAlertView.new  
    alert.message = ‘No Camera in device'
    alert.show
  end

So we have used UIImagePickerControllerSourceTypeCamera and UIImagePickerControllerSourceTypePhotoLibrary as source types; they will open the Camera and Photo Library tools, respectively.

Tip

As an iOS application can also be installed on devices such as an iPod, which does not have a camera, to check the device for a camera, the UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceTypeCamera) method is used.

The following two camera picker delegates are available:

  • imagePickerController:didFinishPickingImage: This is called when the image is selected
  • imagePickerControllerDidCancel: This is called when the Cancel button is clicked

The following delegate will be called when an image is selected:

  def imagePickerController(picker, didFinishPickingImage:image, editingInfo:info)
    self.dismissModalViewControllerAnimated(true)
    @image_view.removeFromSuperview if @image_view
    @image_view = UIImageView.alloc.initWithImage(image)
    @image_view.frame = [[50, 200], [200, 180]]
    view.addSubview(@image_view)
  end

The self.dismissModalViewControllerAnimated(true) method is called explicitly to remove the pop-up, and then the image is displayed using UIImageView.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.14.128.105