iOS 系统图片选择和保存
2015-09-22 20:46
441 查看
#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之时间
- IOS中延时执行的几种方式的比较和汇总
- IOS学习之 NSDate 8小时误差
- ios多线程开发详解
- iOS经典讲解之多线程应用场景
- iOS 测试打包
- iOS-C文件添加到iOS项目中,运行报错
- iOS并发编程指南
- IOS学习之 导航栏
- iOS开发 ---- 其他控件,弹窗,滑块,菊花,步进,分段等
- ios xcode如何修改应用的名称
- iOS开发 ----- Controller之间的传值
- iOS极光推送-具体步骤
- iOS8中Today Extension删除
- IOS去掉NavigationBar底部的那条黑线
- iOS ——点击sectionHeader进行跳转,不同的sectionHeader标签不同
- ios激情详解之GCD
- ios学习--TableView详细解释
- ios 类似QQ登录成功后的提示框
- IO7 和IOS8 弹出透明模态窗体