
效果圖
如圖,通過點(diǎn)擊button進(jìn)入UIalertcontroller,選擇相冊進(jìn)入系統(tǒng)相冊選擇圖片并上傳到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;
//跳轉(zhuǎn)動畫效果
_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:@"相機(jī)" 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];
}
/**
* 調(diào)用照相機(jī)
*/
- (void)openCamera
{
//判斷是否可以打開照相機(jī)
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]){
//攝像頭
_imagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera;
[self presentViewController:_imagePickerController animated:YES completion:nil];
}
else{
NSLog(@"沒有攝像頭");
}
}
/**
* 打開相冊
*/
-(void)openPhotoLibrary{
// 進(jìn)入相冊
if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary])
{
_imagePickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self presentViewController:_imagePickerController animated:YES completion:^{
NSLog(@"打開相冊");
}];
}else{
NSLog(@"不能打開相冊");
}
}
#pragma mark - UIImagePickerControllerDelegate
// 拍照完成回調(diào)
- (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];
}
//進(jìn)入拍攝頁面點(diǎn)擊取消按鈕
- (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用于獲取手機(jī)的相機(jī)和相冊的權(quán)限,運(yùn)行。
需要注意的是,模擬器上并不支持相機(jī),選擇相冊,選擇圖片,完成。

完成.png