Example of iOS album access and photo album Storage
Example of iOS album access and photo album Storage
Effect:
1. Click the "Access album" button to access the system album. After selecting it, it will be displayed in imageView.
2. Click to take a photo and access the camera. Save it in the album and return it to the imageView.
Note: The Camera function must be debugged by a real machine, and cannot be implemented by the simulator.
The simulator has the following effects ~ Pop-up warning box
Download project: github project download link
In this example, two buttons and UIimageView are running in? Http://www.bkjia.com/kf/ware/vc/ "target =" _ blank "class =" keylink "> vcnlib2FyZNbQzO2806O7PC9wPg0KPHA + trim" brush: java; ">@interface ViewController : UIViewController @property (weak, nonatomic) IBOutlet UIImageView *imageShow;@end
ViewController. m
# Pragma mark-photograph and save-(IBAction) takePhotoAction :( id) sender {BOOL isCamera = [UIImagePickerController isCameraDeviceAvailable: UIImagePickerControllerCameraDeviceRear]; if (! IsCamera) {// if not available, the warning box UIAlertView * alert = [[UIAlertView alloc] initWithTitle: @ no available camera message: nil delegate: self cancelButtonTitle: @ confirm otherButtonTitles: nil, nil]; [alert show]; return;} UIImagePickerController * imagePicker = [[UIImagePickerController alloc] init]; imagePicker. sourceType = UIImagePickerControllerSourceTypeCamera;/**** available-> all resource folders UIImagePickerControllerSourceTypeCamera-> camera-> built-in album */imagePicker. delegate = self; // sets the proxy and complies with UINavigationControllerDelegate and UIImagePickerControllerDelegate protocols [self presentViewController: imagePicker animated: YES completion: nil];}
# Pragma mark-access album-(IBAction) browseAlbum :( id) sender {UIImagePickerController * imagePicker = [[UIImagePickerController alloc] init]; imagePicker. sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum; imagePicker. delegate = self; [self presentViewController: imagePicker animated: YES completion: nil];}
# Pragma mark-Protocol method implementation // protocol method. After the selection is completed, it is displayed in imageShow-(void) imagePickerController :( UIImagePickerController *) picker didFinishPickingMediaWithInfo :( NSDictionary *) info {NSLog (@ % @, info); // UIImagePickerControllerMediaType, callback, required NSString * mediaType = info [@ UIImagePickerControllerMediaType]; if ([mediaType isw.tostring: @ public. image]) {// Determine whether the image is UIImage * image = [info objectForKey: UIImagePickerControllerOriginalImage]; self. imageShow. image = image; // determine the sourceType of the picker. if the image is taken, save it to the album if (picker. sourceType = custom) {UIImageWriteToSavedPhotosAlbum (image, self, @ selector (image: didFinishSavingWithError: contextInfo :), nil) ;}// else may certainly be a video, which will not be discussed here ~ The method is similar ~ [Picker dismissViewControllerAnimated: YES completion: nil];} // This method is displayed above UIImageWriteToSavedPhotosAlbum-(void) image :( UIImage *) image didFinishSavingWithError :( NSError *) error contextInfo :( void *) contextInfo {NSLog (@ saved );}