您的位置:首页 > 产品设计 > UI/UE

iOS学习笔记-033.数据的读取——NSString和UIImage的保存

2017-03-04 15:21 573 查看
数据的读取NSString和UIImage的保存
一NSString的保存

二UIImage的保存

三代码

四图示

数据的读取——NSString和UIImage的保存

一、NSString的保存

注意:NSString 保存时需要自定义字符串编码

[string writeToFile:stringPath atomically:YES encoding:NSUTF8StringEncoding error:nil];


二、UIImage的保存

注意:UIImage 保存时需要转为NSData

//把图片数据转为 NSData
NSData * imageData = UIImagePNGRepresentation(image);
//写入
[imageData writeToFile:imagePath atomically:YES];


三、代码

//
//  ViewController.m
//  03_UIView27_NString&UIImage写入
//
//  Created by 杞文明 on 16/1/8.
//  Copyright © 2016年 杞文明. All rights reserved.
//

#import "ViewController.h"

@interface ViewController (){
UIImageView * _imageView;
UIButton * _button;
}

@end

@implementation ViewController

- (void)viewDidLoad {
[super viewDidLoad];
[self createLayout];
[self initData];
}

/**创建布局*/
-(void)createLayout{
//1.创建图片控件
UIImageView * imageView = [[UIImageView alloc]init];
[imageView setFrame:CGRectMake(20, 100, 330, 330)];
[imageView setBackgroundColor:[UIColor grayColor]];
[self.view addSubview:imageView];
_imageView = imageView;

//2.创建按钮
UIButton * button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button setFrame:CGRectMake(80, 460, 200, 40)];
[button setTitle:@"走,添加图片去"  forState:UIControlStateNormal];
[button addTarget:self action:@selector(selectPhoto) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
_button = button;
}

#pragma mark - 初始化数据
-(void)initData{
//1.获取Documents路径
NSArray * document = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

//2.获取图片的路径
NSString * imagePath = [document[0] stringByAppendingPathComponent:@"xm.png"];

//3.加载图片
// 注意:此处不要使用imageNamed方法,因为imageNamed方法是从bundle中加载图像的
UIImage * image = [UIImage imageWithContentsOfFile:imagePath];
if(image!=nil){
[_imageView setImage:image];
}

//4.获取字符
NSString * stringPath = [document[0] stringByAppendingPathComponent:@"xm.txt"];
//看到autoreleasing描述符,需要实例化一个指针,并且传入指针的地址
NSError * err = nil;
NSString * string = [NSString stringWithContentsOfFile:stringPath encoding:NSUTF8StringEncoding error:&err];

//5.打印字符
NSLog(@"string --- %@",string);
}

#pragma mark - 选择照片
-(void)selectPhoto{
UIImagePickerController * picker = [[UIImagePickerController alloc]init];
[picker setAllowsEditing:YES];
[picker setDelegate:self];
[picker setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
[self presentViewController:picker animated:YES completion:nil];
}

#pragma mark - 选择照片的回调
-(void)imagePickerController:(nonnull UIImagePickerController *)picker didFinishPickingMediaWithInfo:(nonnull NSDictionary<NSString *,id> *)info{

NSLog(@"%@",info);
//1.获取到选择的图片
UIImage * image = info[@"UIImagePickerControllerEditedImage"];

//2.把图片显示到imageView上面
[_imageView setImage:image];

//3.把图片数据转为 NSData
NSData * imageData = UIImagePNGRepresentation(image);

//4.获取documents目录
NSArray * document = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

//5.把NSData数据存入到Documents下
NSString * imagePath = [document[0] stringByAppendingPathComponent:@"xm.png"];
[imageData writeToFile:imagePath atomically:YES];

//6.把字符串 “明哥带你去选图” 写入到 xm.txt中
NSString * string = @"明哥带你去选图";
NSString * stringPath = [document[0] stringByAppendingPathComponent:@"xm.txt"];
[string writeToFile:stringPath atomically:YES encoding:NSUTF8StringEncoding error:nil];

//7.把 info 写到 info.plist 中
NSString * infoPath = [document[0] stringByAppendingPathComponent:@"info.plist"];
[info writeToFile:infoPath atomically:YES];

//8.关闭选择照片的 控制器
[picker dismissViewControllerAnimated:YES completion:nil];

NSLog(@"%@",imagePath);
}

@end


四、图示

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: