本文实例为大家分享了ios保存图片到本地的具体代码,供大家参考,具体内容如下
一、工程图
二、代码
rootviewcontroller.h
1
2
3
4
5
6
7
|
#import <uikit/uikit.h> @interface rootviewcontroller : uiviewcontroller { uiimageview *imageview; } @end |
rootviewcontroller.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
|
#import "rootviewcontroller.h" @interface rootviewcontroller () @end @implementation rootviewcontroller - (id)initwithnibname:(nsstring *)nibnameornil bundle:(nsbundle *)nibbundleornil { self = [super initwithnibname:nibnameornil bundle:nibbundleornil]; if (self) { // custom initialization } return self; } - ( void )viewdidload { [super viewdidload]; // do any additional setup after loading the view. //初始化背景图 imageview=[[uiimageview alloc]initwithframe:cgrectmake(100, 100, 100, 100)]; imageview.backgroundcolor=[uicolor redcolor]; [self.view addsubview:imageview]; //将图片保存 [self archive]; //提取保存在本地的图片 [self unarchive]; } #pragma -mark -functions //归档 -( void )archive { nsdata *data=[nskeyedarchiver archiveddatawithrootobject:[uiimage imagenamed:@ "1.jpg" ]]; nsuserdefaults *imagedefault = [nsuserdefaults standarduserdefaults]; [imagedefault setobject:data forkey:@ "image" ]; [imagedefault synchronize]; } //反归档 -( void )unarchive { nsdata* data = [[nsuserdefaults standarduserdefaults]objectforkey:@ "image" ]; id image= [nskeyedunarchiver unarchiveobjectwithdata:data]; imageview.image=image; } |
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。