iOS 系统图片选择和保存

#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.
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值