iOS archive保存圖片到本地的方法
更新時間:2017年03月30日 14:56:41 作者:弦外雨
這篇文章主要為大家詳細介紹了iOS archive保存圖片到本地的方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實例為大家分享了iOS保存圖片到本地的具體代碼,供大家參考,具體內容如下
一、工程圖
二、代碼
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; }
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
iOS 中KVC、KVO、NSNotification、delegate 總結及區(qū)別
這篇文章主要介紹了iOS 中KVC、KVO、NSNotification、delegate 總結及區(qū)別的相關資料,需要的朋友可以參考下2016-10-10iOS應用開發(fā)中的文字選中操作控件UITextView用法講解
這篇文章主要介紹了iOS應用開發(fā)中的文字選中操作控件UITextView用法講解,代碼基于傳統(tǒng)的Objective-C語言,需要的朋友可以參考下2016-02-02