如图,通过点击button进入UIalertcontroller,选择相册进入系统相册选择图片并上传到imageview上显示。话不多说,上代码:
#import "ViewController.m"
#import "ViewController.h"
#import <AVFoundation/AVFoundation.h>
#import <MediaPlayer/MediaPlayer.h>
@interface ViewController ()<UIImagePickerControllerDelegate, UINavigationControllerDelegate>{
UIImagePickerController *_imagePickerController;
}
@property (strong, nonatomic) IBOutlet UIImageView *imgv;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
_imagePickerController = [[UIImagePickerController alloc] init];
_imagePickerController.delegate = self;
//跳转动画效果
_imagePickerController.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
_imagePickerController.allowsEditing = YES;
}
- (IBAction)butt:(id)sender {
UIAlertController *alertCtl =[[UIAlertController alloc]init];
UIAlertAction *cancel =[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
NSLog(@"cancel");
}];
UIAlertAction *xiangji =[UIAlertAction actionWithTitle:@"相机" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
NSLog(@"xiangji");
[self openCamera];
}];
UIAlertAction *xiangce =[UIAlertAction actionWithTitle:@"相册" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
NSLog(@"xiangce");
[self openPhotoLibrary];
}];
[alertCtl addAction:cancel];
[alertCtl addAction:xiangji];
[alertCtl addAction:xiangce];
[self presentViewController:alertCtl animated:YES completion:nil];
}
/**
* 调用照相机
*/
- (void)openCamera
{
//判断是否可以打开照相机
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]){
//摄像头
_imagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera;
[self presentViewController:_imagePickerController animated:YES completion:nil];
}
else{
NSLog(@"没有摄像头");
}
}
/**
* 打开相册
*/
-(void)openPhotoLibrary{
// 进入相册
if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary])
{
_imagePickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self presentViewController:_imagePickerController animated:YES completion:^{
NSLog(@"打开相册");
}];
}else{
NSLog(@"不能打开相册");
}
}
#pragma mark - UIImagePickerControllerDelegate
// 拍照完成回调
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(nullable NSDictionary<NSString *,id> *)editingInfo NS_DEPRECATED_IOS(2_0, 3_0){
NSLog(@"finish..");
if(picker.sourceType == UIImagePickerControllerSourceTypeCamera)
{
//图片存入相册
UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);
}
self.imgv.image =image;
[self dismissViewControllerAnimated:YES completion:nil];
}
//进入拍摄页面点击取消按钮
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{
[self dismissViewControllerAnimated:YES completion:nil];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
接下来在info.plist文件中添加NSPhotoLibraryUsageDescription和NSCameraUsageDescription用于获取手机的相机和相册的权限,运行。
需要注意的是,模拟器上并不支持相机,选择相册,选择图片,完成。
完成.png
网友评论