ios学习笔记——保存图片到相册

最近项目要用到,这是自己练手的程序

 1 //
 2 //  ViewController.m
 3 //  SJZSaveImage
 4 //
 5 //  Created by mac on 16/6/3.
 6 //  Copyright © 2016年 mac. All rights reserved.
 7 //
 8 
 9 #import "ViewController.h"
10 
11 @interface ViewController () <UIActionSheetDelegate>
12 
13 @property (nonatomic, strong) UIImage * image;
14 
15 @end
16 
17 @implementation ViewController
18 
19 - (void)viewDidLoad {
20     [super viewDidLoad];
21     self.title = @"保存图片";
22     
23     UIImage * image = [UIImage imageNamed:@"saveImage.jpg"];
24     self.image = image;
25     
26     UIImageView * imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height / 2)];
27     imageView.center = self.view.center;
28     imageView.image = image;
29     
30     [self.view addSubview:imageView];
31     
32     [self createButton];
33 }
34 
35 - (void)createButton
36 {
37     UIButton * button = [[UIButton alloc] initWithFrame:CGRectMake(40, self.view.frame.size.height * 7 / 8, self.view.frame.size.width - 80, 40)];
38     button.backgroundColor = [UIColor whiteColor];
39     [button setTitle:@"保存图片" forState:UIControlStateNormal];
40     [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
41     button.layer.cornerRadius = 4.0;
42     [button addTarget:self action:@selector(buttonClick) forControlEvents:UIControlEventTouchUpInside];
43     [self.view addSubview:button];
44 }
45 
46 - (void)buttonClick
47 {
48     if([[UIDevice currentDevice].systemVersion doubleValue] > 8.0){
49         //ios8之后用UIAlertController代替UIActionSheet
50         UIAlertController * alert = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
51         
52         //增加保存图片action
53         UIAlertAction *action = [UIAlertAction actionWithTitle:@"保存图片" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
54             
55             //将图片保存到相册,回调函数必须为这个形式
56             UIImageWriteToSavedPhotosAlbum(self.image, self, @selector(image:didFinishSavingWithError:contextInfo:), NULL);
57             
58         }];
59         
60         [action setValue:[UIColor blackColor] forKey:@"_titleTextColor"];
61         [alert addAction:action];
62         
63         //增加取消action
64         UIAlertAction * actionCancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
65             
66         }];
67         [actionCancel setValue:[UIColor redColor] forKey:@"_titleTextColor"];
68         [alert addAction:actionCancel];
69         
70         [self presentViewController:alert animated:YES completion:nil];
71     }else{
72         UIActionSheet * action = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:@"保存图片" otherButtonTitles:nil, nil];
73         [action showInView:self.view];
74     }
75 }
76 
77 - (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
78 {
79     if(buttonIndex == 0){
80         //将图片保存到相册,该函数的回调必须为这种形式
81         UIImageWriteToSavedPhotosAlbum(self.image, self, @selector(image:didFinishSavingWithError:contextInfo:), NULL);
82     }
83 }
84 
85 //保存图片回调函数
86 - (void)image: (UIImage *) image didFinishSavingWithError: (NSError *) error contextInfo: (void *) contextInfo
87 {
88     //我们可以在这里设置弹出框
89     if(error){
90         NSLog(@"保存失败");
91     }else{
92         NSLog(@"保存成功");
93     }
94 }
95 
96 @end

 

posted @ 2016-06-03 22:06  sjzLovecj  阅读(275)  评论(0编辑  收藏  举报