Home > Articles > Mobile Application Development & Programming

  • Print
  • + Share This
Like this article? We recommend

Taking Snapshots

To take a snapshot using the front camera, you'll program the app to do the following:

  • Take a snapshot when the left button is pressed. If the camera preview isn't already showing, show it.
  • Dismiss the camera preview when the right button is pressed.

To do that the programming, add the following statements in bold to the ViewController.swift file:

func takePicture() {
    picker.takePicture()
}

func peripheral(peripheral: CBPeripheral!,
    didUpdateValueForCharacteristic characteristic: CBCharacteristic!,
    error: NSError!) {
    if characteristic.UUID == characteristicKeyPressesUDID {

        var keyPressed:UInt16 = 0

        characteristic.value.getBytes(&keyPressed, length:
sizeof(UInt16))

        //---legends---
        // 1 - right button
        // 2 - left button
        // 0 - button released
        // 3 - both buttons pressed

        println("\(keyPressed)")
        switch (keyPressed) {
        case 0:
            //---a button is lifted---
            self.btnLeft.selected = false
            self.btnRight.selected = false
        case 1:
            //---right button pressed---
            self.btnRight.selected = true
            self.btnLeft.selected = false
            //---hide the camera---
            if picker != nil {
                picker.dismissViewControllerAnimated(true, completion: nil)
            }
        case 2:
            //---left button pressed---
            self.btnLeft.selected = true
            self.btnRight.selected = false
            if picker != nil {
                //---if the camera preview is already showing---
                if picker.view.window != nil {
                    self.takePicture()
                } else {
                    //---show the camera preview---
                    self.displayCamera()
                }
            }
        case 3:
            //---both buttons pressed---
            self.btnLeft.selected = true
            self.btnRight.selected = true
        default:
            return
        }
        return
    }
}

//---save the snapshot taken to the Photos album---
func imagePickerController(picker: UIImagePickerController,
    didFinishPickingMediaWithInfo info: [NSObject : AnyObject]) {
 

    var image = info[UIImagePickerControllerOriginalImage] as UIImage
    UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil)
}

Deploy the application to the device one more time. That's it—go ahead and take all the selfies in the world! The photos you take will now be available in the Photos application on your iPhone.

  • + Share This
  • 🔖 Save To Your Account