IOS accesses the album and saves it to the album. ios accesses the album to take pictures.

Source: Internet
Author: User

IOS accesses the album and saves it to the album. ios accesses the album to take pictures.

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 added to the storyboard;

ViewController. h

@interface ViewController : UIViewController<UINavigationControllerDelegate,UIImagePickerControllerDelegate>@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 pop-up warning box UIAlertView * alert = [[UIAlertView alloc] initWithTitle: @ "no available camera" message: nil delegate: self cancelButtonTitle: @ "OK" 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. im Age "]) {// 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 ");}

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.