关键类
UIImagePickerController
代理
设置两个代理: UINavigationControllerDelegate, UIImagePickerControllerDelegate
照相机权限、相册权限
<key>NSCameraUsageDescription</key>
<string>使用照相机</string>
<key>NSPhotoLibraryUsageDescription</key>
<string>使用相册</string>
sourceType属性
UIImagePickerController的sourceType 一共有三种
typedef NS_ENUM(NSInteger, UIImagePickerControllerSourceType) {
UIImagePickerControllerSourceTypePhotoLibrary, //相簿(这种形式是呈现很多个相册,每个相册里面很多张图片,也就是对应于我们打开系统自带照片App中的第四个底部tab“相簿”的形式)
UIImagePickerControllerSourceTypeCamera, //拍照
UIImagePickerControllerSourceTypeSavedPhotosAlbum //照片 (这种形式会把一张张照片罗列出来,也就是对应于我们打开系统自带照片App中的第一个底部tab“照片”的形式)
} __TVOS_PROHIBITED;
代码效果示例

代码示例
//
// GetAlbumPictureViewController.m
// Helloworld
//
// Created by zhiyunyu on 2018/1/31.
// Copyright © 2018年 zhiyunyu. All rights reserved.
//
#import "GetAlbumPictureViewController.h"
@interface GetAlbumPictureViewController ()<UINavigationControllerDelegate, UIImagePickerControllerDelegate>
@property(nonatomic, strong) UIImageView *avatarImageView;
@end
@implementation GetAlbumPictureViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.view.backgroundColor = [UIColor whiteColor];
self.avatarImageView = ({
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake((self.view.bounds.size.width - 80) / 2, 100, 80, 80)];
imageView.backgroundColor = [UIColor blueColor];
imageView.layer.cornerRadius = imageView.frame.size.width / 2;
imageView.layer.masksToBounds = YES;
NSString *imagePath = [[NSBundle mainBundle] pathForResource:@"avatar" ofType:@"png" inDirectory:@"Image.bundle/home"];
imageView.image = [UIImage imageWithContentsOfFile:imagePath];
imageView;
});
[self.view addSubview:self.avatarImageView];
//打开用户交互
self.avatarImageView.userInteractionEnabled = YES;
//初始化一个手势
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(avatarDidTapped:)];
//为图片添加手势
[ self.avatarImageView addGestureRecognizer:singleTap];
}
//头像点击事件
-(void)avatarDidTapped:(UIGestureRecognizer *) gestureRecognizer{
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"修改头像" message:nil preferredStyle:UIAlertControllerStyleActionSheet];
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"我的相册" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
picker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;//我的相片 (这种形式会把一张张照片罗列出来)
[self presentViewController:picker animated:YES completion:nil];
}];
UIAlertAction *sureAction = [UIAlertAction actionWithTitle:@"拍照" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
[self presentViewController:picker animated:YES completion:nil];
}];
UIAlertAction *destructiveAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
[alertController addAction:cancelAction];
[alertController addAction:sureAction];
[alertController addAction:destructiveAction];
[self presentViewController:alertController animated:YES completion:nil];
}
#pragma mark UINavigationControllerDelegate, UIImagePickerControllerDelegate
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info {
NSString *mediaType = [info objectForKey:UIImagePickerControllerMediaType];
if ([mediaType isEqual:@"public.image"]) {
UIImage *originImage =
[info objectForKey:UIImagePickerControllerOriginalImage];
self.avatarImageView.image = originImage;
}
[self dismissViewControllerAnimated:YES completion:nil];
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
[self dismissViewControllerAnimated:YES completion:nil];
}
@end
网友评论