iOS archive保存圖片到本地的方法
更新時間:2017年03月30日 14:56:41 作者:弦外雨
這篇文章主要為大家詳細(xì)介紹了iOS archive保存圖片到本地的方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實例為大家分享了iOS保存圖片到本地的具體代碼,供大家參考,具體內(nèi)容如下
一、工程圖
二、代碼
RootViewController.h
#import <UIKit/UIKit.h> @interface RootViewController : UIViewController { UIImageView *imageView; } @end
RootViewController.m
#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; }
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
你應(yīng)該知道的tableViewCell行高計算處理
這篇文章主要給大家介紹了關(guān)于tableViewCell行高計算的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-12-12iOS 中KVC、KVO、NSNotification、delegate 總結(jié)及區(qū)別
這篇文章主要介紹了iOS 中KVC、KVO、NSNotification、delegate 總結(jié)及區(qū)別的相關(guān)資料,需要的朋友可以參考下2016-10-10iOS應(yīng)用開發(fā)中的文字選中操作控件UITextView用法講解
這篇文章主要介紹了iOS應(yīng)用開發(fā)中的文字選中操作控件UITextView用法講解,代碼基于傳統(tǒng)的Objective-C語言,需要的朋友可以參考下2016-02-02