UIImagePickerController and UIAlertController are used in combination.
When processing profile pictures, there are usually two options: one is to call the System camera and the other is to call the system album. The UIImagePickerController method is used here.
Add a gesture to the imageView of the Avatar, or add a transparent button to implement the click method.
-(IBAction) click :( id) sender {
// Create a reminder View
UIAlertController * alertController = [UIAlertController alertControllerWithTitle: @ "Reminder" message: nil preferredStyle: UIAlertControllerStyleActionSheet];
UIAlertAction * alertAction = [UIAlertAction actionWithTitle: @ "camera" style: UIAlertActionStyleDestructive handler: ^ (UIAlertAction * action ){
// Determine whether a camera exists on the device. If yes, the system camera is called. If no camera exists, the user is reminded.
If ([UIImagePickerController isSourceTypeAvailable: UIImagePickerControllerSourceTypeCamera]) {
// Create a camera
UIImagePickerController * picker = [[UIImagePickerController alloc] init];
// File Origin
Picker. sourceType = UIImagePickerControllerSourceTypeCamera; // specify the data source from the camera
Picker. delegate = self; // specifies the proxy
Picker. allowsEditing = YES; // allow editing
// Modal pop-up
[Self presentViewController: picker animated: YES completion: nil];
} Else {
// No camera, reminding users that your device does not have a camera
UIAlertController * alertController = [UIAlertController alertControllerWithTitle: @ "your device has no camera" message: nil preferredStyle: UIAlertControllerStyleAlert];
UIAlertAction * alertAction1 = [UIAlertAction actionWithTitle: @ "OK" style: UIAlertActionStyleDestructive handler: nil];
[AlertController addAction: alertAction1];
[Self presentViewController: alertController animated: YES completion: nil];
}
}];
[AlertController addAction: alertAction];
UIAlertAction * alertAction2 = [UIAlertAction actionWithTitle: @ "album" style: UIAlertActionStyleDefault handler: ^ (UIAlertAction * action ){
UIImagePickerController * pickerC = [[UIImagePickerController alloc] init];
PickerC. sourceType = UIImagePickerControllerSourceTypePhotoLibrary; // specify the data source as album
PickerC. delegate = self; // specify the proxy
PickerC. allowsEditing = YES; // allow editing
[Self presentViewController: pickerC animated: YES completion: nil];
}];
[AlertController addAction: alertAction2];
[Self presentViewController: alertController animated: YES completion: nil];
}
// Select the image and run the following command:
-(Void) imagePickerController :( UIImagePickerController *) picker didFinishPickingMediaWithInfo :( NSDictionary *) info {
NSLog (@ "% @", info );
UIImage * image = [info objectForKey: UIImagePickerControllerOriginalImage];
Self. photoImage. image = image;
[Picker dismissViewControllerAnimated: YES completion: nil];
}