IOS Programming Camera 1, iosprogramming

Source: Internet
Author: User

IOS Programming Camera 1, iosprogramming

IOS Programming Camera 1

1 Displaying Images and UIImageView

1.1 put an instance of UIImageView on the screen.

Then drag an instance of UIImageView onto the view and position it below the label.

A UIImageView displays an image according to its contentMode property.

UIImageView displays images based on its contentMode attribute.

This property determines where to position and how to resize the content within the image view's frame.

This attribute determines where and how to re-display the size content in the image View framework.

UIImageView's default value for contentMode is UIViewContentModeScaleToFill, which will adjust the image to exactly match the bounds of the image view.

The default value is UIViewContentModeScaleToFill.

If you keep the default, an image taken by the camera will be contorted to fit into the square UIImageView.

If you use the default settings, your image may be distorted to adapt to the square UIImageView.

@ Property (weak, nonatomic) IBOutlet UIImageView * imageView;

1.2 Adding a camera button

Now you need a button to initiate the photo-taking process.

Now you need a button to initialize photo-taking process.

Instead, you will create an instance of UIToolbar and place it at the bottom of BNRDetailViewController's view.

You need to create a UIToolBar instance and place it at the bottom of BNRDetailViewController's view.


A UIToolbar works a lot like a UINavigationBar-you can add instances of UIBarButtonItem to it.

Where a navigation bar has two slots for bar button items, a toolbar has an array of bar button items. You can place as navigation bar button items in a toolbar as can fit on the screen.

A navigation bar stores bar button items in two locations. a toolbar has a bar button items column. You can store any number of bar button items only on the screen.

By default, a new instance of UIToolbar that is created in a XIB file comes with one UIBarButtonItem.

By default, a UIToolbar already has a UIBarButtonItem.

Select this bar button item and open the attribute inspector. Change the Identifier to Camera, and the item will show a camera icon

Select this bar button item and open attribute inspector. Change Identifier to Camera. A camera flag is displayed.

Select the camera button by first clicking on the toolbar and then the button itself. Then Control-drag from the selected button to the implementation part of BNRDetailViewController. m




2 Taking Pictures and UIImagePickerController

In the takePicture: method, you will instantiate a UIImagePickerController and present it on the screen.

In the takePic, you will create a UIImagePickerController and display it to the screen.

When creating an instance of UIImagePickerController, you must set its sourceType property and assign it a delegate.

When you create a UIImagePickerController instance, you must set the sourceType attribute and assign it a delegate.

2.1 Setting the image picker's sourceType

The sourceType constant that tells the image picker where to get images.

SourceType tells the image picker where to obtain images.

It has three possible values:

(1) UIImagePickerControllerSourceTypeCamera

The user will take a new picture.

The user will get a new image

(2) UIImagePickerControllerSourceTypePhotoLibrary

The user will be prompted to select an album and then a photo from that album.

Select an album and a photo in this albu.


The user picks from the most recently taken photos.

Obtain one from the recently used photos.


The first source type, UIImagePickerControllerSourceTypeCamera, will not work on a device that does not have a camera. so, before using this type, you have to check for a camera by sending the message isSourceTypeAvailable: to the UIImagePickerController class:

The first source type, UIImagePickerControllerSourceTypeCamera, will not be used in devices without cameras. Therefore, before using this type, you need to check that a camera sends isSourceTypeAvailable to UIImagePickerController.

+ (BOOL) isSourceTypeAvailable :( UIImagePickerControllerSourceType) sourceType;


-(IBAction) takePicture :( id) sender

UIImagePickerController * imagePicker =

[[UIImagePickerController alloc] init];

// If the device has a camera, take a picture, otherwise,

// Just pick from photo library

If ([UIImagePickerController

IsSourceTypeAvailable: UIImagePickerControllerSourceTypeCamera]) {

ImagePicker. sourceType = UIImagePickerControllerSourceTypeCamera;

} Else {

ImagePicker. sourceType = UIImagePickerControllerSourceTypePhotoLibrary;



2.2 Setting the image picker's delegate

Set the delegate of image picker

When the user selects an image from the UIImagePickerController's interface, the delegate is sent the message imagePickerController: didFinishPickingMediaWithInfo :. (If the user taps the cancel button, then the delegate parameters es the message imagePickerControllerDidCancel :.)

When you select an image from the UIImagePickerController's interface, the delegate (received message) is sent to imagePickerController: didFinishPickingMediaWithInfo. (If you select the cancel button, delegate will receive the imagePickerControllerDidCancel message .)

The image picker's delegate will be the instance of BNRDetailViewController.

@ Interface BNRDetailViewController ()
<UINavigationControllerDelegate, UIImagePickerControllerDelegate>

Why UINavigationControllerDelegate?

Why is there UINavigationControllerDelegate?

UIImagePickerController's delegate property is actually inherited from its superclass, role, and while UIImagePickerController has its own delegate protocol, its inherited delegate property is declared to point to an object that conforms to users.

The UIImagePickerController's delegate attribute actually inherits from its super class UINavigationController. When UIImagePickerController has its own delegate protocol, its inherited delegate attribute is directed to an object subject to UINavigationControllerDelegate.

ImagePicker. delegate = self;


2.3 Presenting the image picker modally

Once the UIImagePickerController has a source type and a delegate, it is time to get its view on the screen.

Once UIImagePickerController has source type and delegate, the view should be displayed on the screen.

Unlike other UIViewController subclasses you have used, an instance of UIImagePickerController is presented modally.

Unlike other subclass of UIViewController, the instance of UIImagePickerController shows modally.

A modal view controller takes over the entire screen until it has finished its work.

A modal view controller occupies the entire screen until it completes its work.

To present a view controller modally, you send presentViewController: animated: completion: to the UIViewController whose view is on the screen.

You need to send presentViewController: animated: completion to the UIViewController whose view is on the screen.

// Place image picker on the screen
[Self presentViewController: imagePicker animated: YES completion: nil];

2.4 Saving the image

You do not have a reference to the photo once the image picker is dismissed. to fix this, you are going to implement the delegate method imagePickerController: didFinishPickingMediaWithInfo :. this message is sent to the image picker's delegate when a photo has been selected.

You need to implement the delegate method: imagePickerController: didFinishPickingMediaWithInfo. When a photo is selected, this method is sent to the image picker delegate.

-(Void) imagePickerController :( UIImagePickerController *) picker didFinishPickingMediaWithInfo :( NSDictionary *) info

// Get picked image from info dictionary
UIImage * image = info [UIImagePickerControllerOriginalImage];

// Put that image onto the screen in our image view

Self. imageView. image = image;

// Take image picker off the screen-
// You must call this dismiss method
[Self dismissViewControllerAnimated: YES completion: nil];



The solution, which you are going to implement in the next section, is to store images to disk and only fetch them into RAM when they are needed.

The solution is to store images to disks and create them when they are used.







Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.