Ios sdk details: Photo/album (default + custom photo Interface), iossdk
Original blog, reprinted, please indicate the source
Blog.csdn.net/hello_hwc
Preface:
I was going to update the UploadTask of NSURLSession. When I wrote the Demo, I wanted to write it as a photo for upload. Then I thought I would like to write a Demo about the photo first. This article describes how to use the system-provided interface to take photos and select albums, and then customize the photo interface. Note: This article uses UIImagePickerController, so it cannot be completely customized. If you want to completely customize the photo, we recommend that you use the AV Foundation framework.
Demo Effect
Go to the system photo page
Go to the custom photo page
Custom Animation for switching between front camera and rear camera-flip page
1. Use the system-provided interface for photo taking and album Selection
Step 1
Save a UIImagePickerController instance and initialize it as appropriate. Demo: Initialize viewDidLoad. Implement UIImagePickerControllerDelegate and UINavigationControllerDelegate for the current class.
@ Property (strong, nonatomic) UIImagePickerController * imagePikerViewController; // initialize self. imagePikerViewController = [[UIImagePickerController alloc] init]; self. imagePikerViewController. delegate = self; // transmits the picture self as a proxy. imagePikerViewController. allowsEditing = YES; // edit allowed
Step 2: Use ActionSheet to select whether to take a photo or album, and then display the mode.
[self presentViewController:self.imagePikerViewController animated:YES completion:NULL];
Note: you must first determine whether the camera is available and then enter the camera (the camera may be broken or running on the virtual machine)
UIAlertController * alertController = [UIAlertController alertControllerWithTitle: nil message: nil preferredStyle:UIAlertControllerStyleActionSheet]; [alertController addAction: [UIAlertAction actionWithTitle: @"Take Photo" style: UIAlertActionStyleDefault handler:^(UIAlertAction *action) { if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]){ self.imagePikerViewController.sourceType = UIImagePickerControllerSourceTypeCamera; [self presentViewController:self.imagePikerViewController animated:YES completion:NULL]; }else{ [self showAlertWithMessage:@"Camera is not available in this device or simulator"]; } }]]; [alertController addAction: [UIAlertAction actionWithTitle: @"Choose Existing Photo" style: UIAlertActionStyleDefault handler:^(UIAlertAction *action) { if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]){ self.imagePikerViewController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary; [self presentViewController:self.imagePikerViewController animated:YES completion:NULL]; } }]]; [alertController addAction: [UIAlertAction actionWithTitle: @"Cancel" style: UIAlertActionStyleCancel handler:nil]]; [self presentViewController: alertController animated: YES completion: nil];
Part 3: The proxy function handles the photo or cancel event
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{ UIImage * image = info[UIImagePickerControllerEditedImage]; if (!image) { image = info[UIImagePickerControllerOriginalImage]; } self.imageview.image = image; [self dismissViewControllerAnimated:YES completion:NULL];}-(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{ [self dismissViewControllerAnimated:YES completion:NULL];}
Ii. Custom photo page
The custom interface of UIImagePickerController is relatively simple. You can set the cameraOverlayView attribute to custom View.
Step 1 create a View
Create an xib File
Drag the control and perform autoLayout.
Set fileOwner to CustomTakePhotoViewController
Step 2: Set the UI to what you want before displaying the photo interface. Note that the attributes
ShowsCameraControls is set to NO to prevent the default Interface from appearing.
self.imagePikerViewController.sourceType = UIImagePickerControllerSourceTypeCamera;self.imagePikerViewController.showsCameraControls = NO;[[NSBundle mainBundle] loadNibNamed:@"CustomOverLayview" owner:self options:nil];self.takePictureButton.layer.cornerRadius = 20;self.takePictureButton.clipsToBounds = YES;self.overlayView.frame = self.imagePikerViewController.cameraOverlayView.frame;self.overlayView.backgroundColor = [UIColor clearColor];self.imagePikerViewController.cameraOverlayView = self.overlayView;self.overlayView = nil;[self presentViewController:self.imagePikerViewController animated:YES completion:NULL];
Step 3: Add actions for controls on the view
Segment Control is responsible for switching between the front camera and the rear camera. To ensure smoothness, the animation is displayed during switching.
- (IBAction)cameraSelect:(UISegmentedControl *)sender{ NSInteger index = sender.selectedSegmentIndex; if (index == 0) { [UIView transitionWithView:self.imagePikerViewController.view duration:1.0 options:UIViewAnimationOptionAllowAnimatedContent | UIViewAnimationOptionTransitionCurlDown animations:^{ self.imagePikerViewController.cameraDevice = UIImagePickerControllerCameraDeviceFront; } completion:NULL]; }else{ [UIView transitionWithView:self.imagePikerViewController.view duration:1.0 options:UIViewAnimationOptionAllowAnimatedContent | UIViewAnimationOptionTransitionCurlUp animations:^{ [self.imagePikerViewController setCameraDevice:UIImagePickerControllerCameraDeviceRear]; } completion:NULL]; }}
Photo Button
- (IBAction)takePicture:(id)sender { [self.imagePikerViewController takePicture];}
Canceled Button
- (IBAction)cancelTakePicture:(id)sender { [self dismissViewControllerAnimated:YES completion:NULL];}
Step 4 process images in the proxy
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{ UIImage * image = info[UIImagePickerControllerEditedImage]; if (!image) { image = info[UIImagePickerControllerOriginalImage]; } self.imageview.image = image; [self dismissViewControllerAnimated:YES completion:NULL];}
Note that if the log output
Snapshotting a view that has not been rendered results in an empty snapshot. Ensure your view has been rendered at least once before snapshotting or snapshot after screen updates.
Ignore it directly without any impact. It seems to be a bug in IOS 8.
Download link
Http://download.csdn.net/detail/hello_hwc/8553539