IOS UIImagePickerController obtains pictures from photos, galleries and albums

Time:2021-10-22

IOS UIImagePickerController obtains pictures from photos, galleries and albums

There are three ways for IOS to get pictures:

1. Directly call the camera to take photos

2. Select from album

3. Select from Gallery

UIImagePickerController is the interface provided by the system to obtain pictures and videos;

Using UIImagePickerController class to obtain pictures and videos can be divided into the following steps:

1. Initialize UIImagePickerController class;

2. Set the data source type of UIImagePickerController instance (explained below);

3. Set up agent;

4. If the picture needs to be modified, set allowsediting = yes.

There are three types of data sources:

enum {

  Uiimagepickercontrollersourcetypephotolibrary, // from Gallery

  Uiimagepickercontrollersourcetypecamera, // from camera

  Uiimagepickercontrollersourcetypesavedphotosalbum // from album

};

When using these sources, it is best to check whether the following equipment supports;

if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])

  {

    Nslog (@ "camera support");

  }

  if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary])

  {

    Nslog (@ "support Gallery");

  }

  if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeSavedPhotosAlbum])

  {

    Nslog (@ "photo gallery supported");

  }

Call the camera to get resources


- (void)viewDidLoad {

  [super viewDidLoad];

  picker = [[UIImagePickerController alloc]init];

  picker.view.backgroundColor = [UIColor orangeColor];

  UIImagePickerControllerSourceType sourcheType = UIImagePickerControllerSourceTypeCamera;

  picker.sourceType = sourcheType;

  picker.delegate = self;

  picker.allowsEditing = YES;

}

The above is just an example of UIImagePickerController and its properties. When you need to get a picture, you need to call a pop-up window


[self presentViewController:picker animated:YES completion:nil];

We also need an agent to get the pictures we selected


UIImagePickerControllerDelegate

There are three methods in the agent, one of which 3.0 has been abandoned, and there are only two methods we need to use


- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary

 *)info;

When the user is selected, it is called.


- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker;

Called when the user cancels the selection;


- (void)imagePickerController:(UIImagePickerController *)picker

 didFinishPickingMediaWithInfo:(NSDictionary *)info;

The selected information is in info, which is a dictionary.

Key in Dictionary:

NSString *const UIImagePickerControllerMediaType ; Specify the media type selected by the user (extended at the end of the article)

NSString *const UIImagePickerControllerOriginalImage ; Original picture

NSString *const UIImagePickerControllerEditedImage ; Modified picture

NSString *const UIImagePickerControllerCropRect ; Cutting size

NSString *const UIImagePickerControllerMediaURL ; URL of the media

NSString *const UIImagePickerControllerReferenceURL ; URL of the original

NSString *const UIImagePickerControllerMediaMetadata; This value is valid only when the incoming data source is a camera

Uiimagepickercontrollermediatype contains kuttypeimage and kuttypemovie

Kuttypeimage contains:

const CFStringRef kUTTypeImage ; Abstract picture type

const CFStringRef kUTTypeJPEG ;

const CFStringRef kUTTypeJPEG2000 ;

const CFStringRef kUTTypeTIFF ;

const CFStringRef kUTTypePICT ;

const CFStringRef kUTTypeGIF ;

const CFStringRef kUTTypePNG ;

const CFStringRef kUTTypeQuickTimeImage ;

const CFStringRef kUTTypeAppleICNS 

const CFStringRef kUTTypeBMP;

const CFStringRef kUTTypeICO;

Kuttypemovie contains:

const CFStringRef kUTTypeAudiovisualContent ; Abstract sound and video

const CFStringRef kUTTypeMovie ; Abstract media formats (sound and video)

const CFStringRef kUTTypeVideo ; Only video, no sound

const CFStringRef kUTTypeAudio ; Only sound, no video

const CFStringRef kUTTypeQuickTimeMovie ;

const CFStringRef kUTTypeMPEG ;

const CFStringRef kUTTypeMPEG4 ;

const CFStringRef kUTTypeMP3 ;

const CFStringRef kUTTypeMPEG4Audio ;

const CFStringRef kUTTypeAppleProtectedMPEG4Audio;

If you have any questions, please leave a message or go to the community of this site for exchange and discussion. Thank you for reading. I hope it can help you. Thank you for your support to this site!