ios学习笔记——保存图片到相册
2016-06-03 22:06
465 查看
最近项目要用到,这是自己练手的程序
// // ViewController.m // SJZSaveImage // // Created by mac on 16/6/3. // Copyright © 2016年 mac. All rights reserved. // #import "ViewController.h" @interface ViewController () <UIActionSheetDelegate> @property (nonatomic, strong) UIImage * image; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; self.title = @"保存图片"; UIImage * image = [UIImage imageNamed:@"saveImage.jpg"]; self.image = image; UIImageView * imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height / 2)]; imageView.center = self.view.center; imageView.image = image; [self.view addSubview:imageView]; [self createButton]; } - (void)createButton { UIButton * button = [[UIButton alloc] initWithFrame:CGRectMake(40, self.view.frame.size.height * 7 / 8, self.view.frame.size.width - 80, 40)]; button.backgroundColor = [UIColor whiteColor]; [button setTitle:@"保存图片" forState:UIControlStateNormal]; [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; button.layer.cornerRadius = 4.0; [button addTarget:self action:@selector(buttonClick) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:button]; } - (void)buttonClick { if([[UIDevice currentDevice].systemVersion doubleValue] > 8.0){ //ios8之后用UIAlertController代替UIActionSheet UIAlertController * alert = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet]; //增加保存图片action UIAlertAction *action = [UIAlertAction actionWithTitle:@"保存图片" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) { //将图片保存到相册,回调函数必须为这个形式 UIImageWriteToSavedPhotosAlbum(self.image, self, @selector(image:didFinishSavingWithError:contextInfo:), NULL); }]; [action setValue:[UIColor blackColor] forKey:@"_titleTextColor"]; [alert addAction:action]; //增加取消action UIAlertAction * actionCancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) { }]; [actionCancel setValue:[UIColor redColor] forKey:@"_titleTextColor"]; [alert addAction:actionCancel]; [self presentViewController:alert animated:YES completion:nil]; }else{ UIActionSheet * action = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:@"保存图片" otherButtonTitles:nil, nil]; [action showInView:self.view]; } } - (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex { if(buttonIndex == 0){ //将图片保存到相册,该函数的回调必须为这种形式 UIImageWriteToSavedPhotosAlbum(self.image, self, @selector(image:didFinishSavingWithError:contextInfo:), NULL); } } //保存图片回调函数 - (void)image: (UIImage *) image didFinishSavingWithError: (NSError *) error contextInfo: (void *) contextInfo { //我们可以在这里设置弹出框 if(error){ NSLog(@"保存失败"); }else{ NSLog(@"保存成功"); } } @end
相关文章推荐
- Ios开发:改变线程优先级
- iOS的动画效果类型及实现方法
- iOS学习笔记-----GCD 用法介绍
- iOS 之 arc4random() 和 arc4random_uniform()的区别
- 如何做好IOS View的布局
- iOS开发之LLDB打印json数据显示中文
- iOS开发一些小技巧
- iOS Layout 技术回顾及 Masonry 介绍
- iOS开发 纯代码屏幕适配
- iOS下dyld: Library not loaded: 错误解决方法
- CAShapeLayer的简单实用(2)
- 关于 timeIntervalSince1970 计算出错的问题(4s)
- Pod管理的iOS项目修改工程名
- iOS8 tableview separatorInset cell分割线左对齐,ios7的方法失效了
- Block中对weakSelf进行strong处理
- 如果爱,请深爱:10大iOS开发者最喜爱的类库
- iOS简单、方便、好用的HZSigmentScrollView(二)
- iOS navigationbar的各种改色
- iOS 传值方法(属性传值、代理传值、Block、通知、单例)
- 在iOS中动态改变statusbar的颜色