IOS development-iOS calls the camera to call album [save images to a local album], ios-ios
Set the header proxy
<UINavigationControllerDelegate, UIImagePickerControllerDelegate>
1. Call the camera
Check whether the front camera is available
- (BOOL)isFrontCameraAvailable{ return [UIImagePickerController isCameraDeviceAvailable:UIImagePickerControllerCameraDeviceFront];}
Check whether the rear camera is available
- (BOOL)isRearCameraAvailable{ return [UIImagePickerController isCameraDeviceAvailable:UIImagePickerControllerCameraDeviceRear];}
Call the camera
/// Call the camera-(void) persentImagePicker {if (! _ ImagePickerG) {// initialize the camera _ imagePickerG = [[UIImagePickerController alloc] init]; // proxy _ imagePickerG. delegate = self;} // whether the front camera is available if ([self isFrontCameraAvailable]) {_ imagePickerG. sourceType = UIImagePickerControllerSourceTypeCamera;} // whether the camera is available with else if ([self isFirstResponder]) {_ imagePickerG. sourceType = UIImagePickerControllerSourceTypeCamera;} else {[SVProgressHUD showErrorWithStatus: @ "No camera available ~ "]; Return;} // crop _ imagePickerG. allowsEditing = YES; [self. navigationController presentViewController: _ imagePickerG animated: YES completion: nil];}
2. Call album
/// Call the local album-(void) persentImagePicker {if (! _ ImagePickerG) {// initialize the camera _ imagePickerG = [[UIImagePickerController alloc] init]; // proxy _ imagePickerG. delegate = self;} // album _ imagePickerG. sourceType = UIImagePickerControllerSourceTypePhotoLibrary; _ imagePickerG. allowsEditing = YES; [self. navigationController presentViewController: _ imagePickerG animated: YES completion: nil];}
3. Proxy events
/// Deselect the image (photograph)-(void) imagePickerControllerDidCancel :( UIImagePickerController *) picker {[picker dismissViewControllerAnimated: YES completion: nil];}
/// Select image completion (completed from album or photograph)-(void) imagePickerController :( UIImagePickerController *) picker didFinishPickingMediaWithInfo :( NSDictionary <NSString *, id> *) info {UIImage * image = [info objectForKey: UIImagePickerControllerOriginalImage]; // source image // obtain the cropped image UIImage * imageUp = [info objectForKey: UIImagePickerControllerEditedImage];}
4. Save the image to your local device.
/// Save the image to a local album-(void) imageTopicSave :( UIImage *) image {UIImageWriteToSavedPhotosAlbum (image, self, @ selector (image: didFinishSavingWithError: contextInfo :), nil );} -(void) image :( UIImage *) image didFinishSavingWithError :( NSError *) error contextInfo :( void *) contextInfo {if (error = nil) {} else {// The image cannot be saved locally }}