#import "ViewController.h"
//因为会用到代理 所以要服从下面三个协议
@interface ViewController ()<UIActionSheetDelegate ,UIImagePickerControllerDelegate,UINavigationControllerDelegate>
@property (weak, nonatomic) IBOutlet UIImageView *picView;
@end
@implementation ViewController
//出现下拉菜单(这是一个button的点击事件)
- (IBAction)ButtonAction:(id)sender {
//创建一个UIAlertController 我们可以有选择的显示提示button
UIAlertController *alert=[UIAlertController alertControllerWithTitle:@"选择" message:nil preferredStyle:UIAlertControllerStyleActionSheet];
//拍照
//先检测是否有拍照功能,如果没有(为xcode模拟器) 不会弹出拍照按钮
if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]){
UIAlertAction *action1=[UIAlertAction actionWithTitle:@"拍照" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction *action) {
[self chioceSourceType:UIImagePickerControllerSourceTypeCamera];
}];
[alert addAction:action1];
}
//相册
UIAlertAction *action2=[UIAlertAction actionWithTitle:@"从相册中选择" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction *action) {
[self chioceSourceType:UIImagePickerControllerSourceTypeSavedPhotosAlbum];
}];
//取消
UIAlertAction *action3=[UIAlertAction actionWithTitle:@"取消" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction *action) {
}];
[alert addAction:action2];
[alert addAction:action3];
[self presentViewController:alert animated:YES completion:^{
}];
}
//选择模式
-(void)chioceSourceType:(NSInteger)sourceType{
UIImagePickerController *imagePicker=[[UIImagePickerController alloc]init];
//设置代理
imagePicker.delegate=self;
imagePicker.allowsEditing=YES;
imagePicker.sourceType=sourceType;
[self presentViewController:imagePicker animated:YES completion:^{
}];
}
//选择图片完成后的方法
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
//截取图片后执行的方法
[picker dismissViewControllerAnimated:YES completion:^{
}];
UIImage *image=[info objectForKey:UIImagePickerControllerOriginalImage];
UIImageWriteToSavedPhotosAlbum(image, self, nil, NULL);
self.picView.image=image;
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
iOS 系统图片选择和保存
最新推荐文章于 2022-11-12 20:56:19 发布