banner



How To Open Camera Xcode


Learn SwiftUI and have your iOS Development to the Next Level
SwiftUI Essentials – iOS 14 Edition book is now available in Print ($39.99) and eBook ($29.99) editions. Learn more...

Buy Print Preview Book


The iOS iv SDK provides access to both the iPhone photographic camera device and photo library through the UIImagePickerController class. This allows videos and photographs to be taken from within an iPhone awarding and for existing photos and videos to be presented to the user for option.

This affiliate will cover the basics and some of the theory of using the UIImagePickerController form before working through the step by footstep creation of an example application in An Example iOS 4 iPhone Camera Application.

Contents


The iOS 4 UIImagePickerController Course

The ultimate purpose of the UIImagePickerController class is to provide applications with either an epitome or video. It achieves this task by providing the user with admission to the camera, camera roll and photo libraries on the device. In the case of the camera, the user is able to either take a photo or record a video depending on the capabilities of the device and the application'southward configuration of the UIImagePickerController object. In terms of photographic camera roll and library access, the object provides the application with the existing image or video selected by the user. The controller too allows new photos and videos created inside the application to exist saved to the library.

Ezoic

Creating and Configuring a UIImagePickerController Instance

<google>IOSBOX</google> In club to use the UIImagePickerController, an instance of the class must first be created. In addition, properties of the instance demand to be configured to command the source for the images or videos (photographic camera, photographic camera curlicue or library). Further, the types of media that are acceptable to the awarding must also be defined (photos, videos or both). Some other configuration option defines whether the user has the selection to edit a photo once it has been taken and before it is passed to the awarding. The source of the media is defined past setting the sourceType property of the UIImagePickerController object to one of the iii supported types:

  • UIImagePickerControllerSourceTypeCamera
  • UIImagePickerControllerSourceTypeSavedPhotosAlbum
  • UIImagePickerControllerSourceTypePhotoLibrary

The types of media adequate to the application are divers by setting the mediaTypes property, an NSArray object that tin can be configured to back up both video and images. The KUTTypeImage and KUTTypeMove definitions contained in the <MobileCoreServices/MobileCoreServices.h> include file can be used equally values when configuring this property. Whether or not the user is permitted to perform editing earlier the image is passed on to the application is controlled via the allowsEditing boolean belongings.

The following code creates a UImagePickerController instance and configures information technology for camera use with motion picture and image support and editing allowed. It then displays the controller and releases the controller object:

UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];  imagePicker.delegate = self;  imagePicker.sourceType =          UIImagePickerControllerSourceTypeCamera;  imagePicker.mediaTypes = [NSArray arrayWithObjects:         (NSString *) kUTTypeImage,         (NSString *) kUTTypeMovie, nada];  imagePicker.allowsEditing = Yes; [self presentModalViewController:imagePicker          blithe:YES]; [imagePicker release];        

It should be noted that the above lawmaking also configured the current class as the consul for the UIImagePickerController case. This is actually a cardinal part of how the course works and is covered in the next section.


Configuring the UIImagePickerController Delegate

When the user is presented with the UIImagePickerController object user interface the application essentially hands control to that object. That existence the case, the controller needs some way to notify the awarding that the user has taken a photograph, recorded a video or fabricated a library selection. It does this by calling delegate methods. The form that instantiates a UIImagePickerController instance should, therefore, declare itself equally the object's delegate, conform to the UIImagePickerControllerDelegate and UINavigationControllerDelegate protocols and implement the didFinishPickingMediaWithInfo: and imagePickerControllerDidCancel: methods. When the user has selected or created media, the didFinishPickingMediaWithInfo method is called and passed an NSDictionary object containing the media and associated data. In the event that the user cancels the operation the imagePickerControllerDidCancel method is called. In both cases it is the responsibleness of the consul method to dismiss the view controller:

-(void)imagePickerController: (UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {      // Code here to work with media      [self dismissModalViewControllerAnimated:YES]; }  -(void)imagePickerControllerDidCancel: (UIImagePickerController *)picker {      [self dismissModalViewControllerAnimated:YES]; }        

The info argument passed to the didFinishPickingMediaWithInfo method is an NSDictionary object containing the data relating to the image or video created or selected by the user. The first step is typically to place the type of media:

NSString *mediaType =    [info objectForKey:UIImagePickerControllerMediaType];  if ([mediaType isEqualToString:(NSString *)kUTTypeImage])  { 	// Media is an image } else if ([mediaType isEqualToString:(NSString *)kUTTypeMovie]) { 	// Media is a video }        

The original, unedited image selected or photographed by the user may exist obtained from the info dictionary as follows:

UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];        

Assuming that editing was enabled on the image picker controller object, the edited version of image may be accessed via the UImagePickerControllerEditedImage dictionary key:

UIImage *image = [info objectForKey:UIImagePickerControllerEditedImage];        

If the media is a video, the URL of the recorded media may be accessed as follows:

NSURL *url = [info objectForKey:UIImagePickerControllerMediaURL];        

Once the paradigm or video URL has been obtained the application can optionally save the media to the library and either display the image to the user or play the video using the MPMoviePlayer form as outlined the affiliate entitled Video Playback from within an iOS iv iPhone Application.

Detecting Device Capabilities

Not all iOS devices provide the aforementioned functionality. iPhone models prior to the 3GS model, for case, practise not support the recording of video. Some iPod Touch models do not accept a photographic camera so neither the camera, nor camera scroll are available via the prototype picker controller. These differences in functionality make it important to detect the capabilities of a device when using the UIImagePickerController class. Fortunately, this may easily be achieved past a call to the isSourceTypeAvailable class method of the UIImagePickerController. For example, to detect the presence of a camera:

if ([UIImagePickerController isSourceTypeAvailable:           UIImagePickerControllerSourceTypeCamera]) { 	// code hither }        

Similarly, to examination for admission to the camera coil:

if ([UIImagePickerController isSourceTypeAvailable:      UIImagePickerControllerSourceTypeSavedPhotosAlbum]) { 	// code hither }        

Finally, to check for support for photo libraries:

if ([UIImagePickerController isSourceTypeAvailable:      UIImagePickerControllerSourceTypePhotoLibrary]) { 	// code here }        

Saving Movies and Images

Once a video or photo created past the user using the camera is handed off to the application it is then the responsibleness of the application code to save that media into the library. Photos and videos may be saved via calls to the UIImageWriteToSavedPhotosAlbum and UISaveVideoAtPathToSavedPhotosAlbum methods respectively. These methods use a target-action mechanism whereby the save action is initiated and the application continues to run. When the action is complete a specified method is called to notify the application of the success or otherwise of the operation.

To save an image:

UIImageWriteToSavedPhotosAlbum(image, self,    @selector(image:finishedSavingWithError:contextInfo:),    aught);        

To save a video:

if (UIVideoAtPathIsCompatibleWithSavedPhotosAlbum(videoPath)) {      UISaveVideoAtPathToSavedPhotosAlbum(videoPath,           self,           @selector(video:finishedSavingWithError:contextInfo:),           nil); }        

Finally, the finishedSavingWithError method that will be called when the action is either consummate or failed due to an mistake:

-(void)image:(UIImage *)image finishedSavingWithError:(NSError *) fault contextInfo:(void *)contextInfo {     if (error) {          UIAlertView *alert = [[UIAlertView alloc]          initWithTitle: @"Save failed"          message: @"Failed to save image/video"          delegate: nil          cancelButtonTitle:@"OK"          otherButtonTitles:nil];          [alert show];          [warning release];     } }        

Summary

In this chapter nosotros have provided an overview of the UIImagePickerController and looked at how this grade can be used either to allow a user to have a picture show or record video from within an iPhone application or select media from the device photo libraries. Now that the theory has been covered the next chapter entitled An Instance iOS iv iPhone Camera Awarding will work through the evolution of an example application that implements the theory covered in this chapter.

Learn SwiftUI and have your iOS Development to the Adjacent Level
SwiftUI Essentials – iOS fourteen Edition volume is now available in Print ($39.99) and eBook ($29.99) editions. Learn more...

Buy Print Preview Book


Source: https://www.techotopia.com/index.php/Accessing_the_iPhone_Camera_and_Photo_Library_%28Xcode_4%29

Posted by: hernandezmakentance.blogspot.com

0 Response to "How To Open Camera Xcode"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel