SoFunction
Updated on 2025-04-08

IOS UIImagePickerController obtains pictures from taking photos, galleries, and albums

IOS UIImagePickerController obtains pictures from taking photos, galleries, and albums

There are three ways to get pictures on iOS:

1. Call the camera directly to take photos

2. Select from the album

3. Select from the gallery

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

Use the UIImagePickerController class to obtain pictures and videos, which are generally divided into the following steps:

1. Initialize the UIImagePickerController class;

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

3. Set up the proxy;

4. If you need to modify the image, set allowEditing =yes.

There are three types of data source:

enum {

  UIImagePickerControllerSourceTypePhotoLibrary ,//From the gallery
  UIImagePickerControllerSourceTypeCamera ,//From the camera
  UIImagePickerControllerSourceTypeSavedPhotosAlbum //From the album
};

When using these sources, it is best to check whether the following devices support it;

 if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])

  {

    NSLog(@"Support camera");

  }

  if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary])

  {

    NSLog(@"Support Gallery");

  }

  if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeSavedPhotosAlbum])

  {

    NSLog(@"Support photo library");

  }

Call the camera to get resources

- (void)viewDidLoad {

  [super viewDidLoad];

  picker = [[UIImagePickerController alloc]init];

   = [UIColor orangeColor];

  UIImagePickerControllerSourceType sourcheType = UIImagePickerControllerSourceTypeCamera;

   = sourcheType;

   = self;

   = YES;

}

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

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

We also need a proxy to get the image we selected

UIImagePickerControllerDelegate

There are three methods in the proxy. One of them is 3.0, and there are only two that we need to use.

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

 *)info;

Called when the user selects it completes;

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

Called when the user unselects;

- (void)imagePickerController:(UIImagePickerController *)picker

 didFinishPickingMediaWithInfo:(NSDictionary *)info;

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

Keys in the dictionary:

NSString *const UIImagePickerControllerMediaType ;Specify the media type selected by the user(The article is expanded at the end)

NSString *const UIImagePickerControllerOriginalImage ;Original image

NSString *const UIImagePickerControllerEditedImage ;Modified pictures

NSString *const UIImagePickerControllerCropRect ;Cut size

NSString *const UIImagePickerControllerMediaURL ;MediaURL

NSString *const UIImagePickerControllerReferenceURL ;OriginalURL

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

UIImagePickerControllerMediaType contains KUTTypeImage and KUTTypeMovie

KUTTypeImage contains:

const CFStringRef kUTTypeImage ;Abstract picture types

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 video

const CFStringRef kUTTypeMovie ;Abstract media format(Sound and video)

const CFStringRef kUTTypeVideo ;Only videos, no sounds

const CFStringRef kUTTypeAudio ;Only sounds, no videos

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 to exchange and discuss. Thank you for reading. I hope it can help you. Thank you for your support for this site!