//
// ViewController.h
// 写入UIImage和NSString
//
// Created by jay on 15/12/6.
// Copyright (c) 2015年 jay. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController<UINavigationControllerDelegate,UIImagePickerControllerDelegate>
@end
//
// ViewController.m
// 写入UIImage和NSString
//
// Created by jay on 15/12/6.
// Copyright (c) 2015年 jay. All rights reserved.
//
#import "ViewController.h"
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIImageView *imageView;
- (IBAction)pickerBtn:(UIButton *)sender;
@property (weak, nonatomic) IBOutlet UITextView *txt;
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
NSArray *documents=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *path=[documents[0] stringByAppendingPathComponent:@"image.png"];
UIImage *image=[UIImage imageNamed:path];
if (image) {
[self.imageView setImage:image];
}
NSString *strPath=[documents[0] stringByAppendingPathComponent:@"1.txt"];
NSString *str=[NSString stringWithContentsOfFile:strPath encoding:NSUTF8StringEncoding error:nil];
if (str) {
[self.txt setText:str];
}
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
- (IBAction)pickerBtn:(UIButton *)sender
{
//创建UIImagePickerController
UIImagePickerController *imagePicker=[[UIImagePickerController alloc]init];
//被选择的照片来源
[imagePicker setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
//开启编辑模式
[imagePicker setAllowsEditing:YES];
//设置代理
[imagePicker setDelegate:self];
//显示UIImagePickerController
[self presentViewController:imagePicker animated:YES completion:nil];
}
#pragma mark-选择完照片后回调
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
NSLog(@"%@",info);
//UIImagePickerControllerOriginalImage
//得到选择的图片
UIImage *image=info[@"UIImagePickerControllerOriginalImage"];
//设置UIImageView图片内容
[self.imageView setImage:image];
//获取沙盒内的document目录
NSArray *documents=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
//构建文件路径
NSString *path=[documents[0] stringByAppendingPathComponent:@"image.png"];
//将图片转化为二进制数据
NSData *imageData=UIImagePNGRepresentation(image);
//将文件写到沙盒内的document目录里
[imageData writeToFile:path atomically:YES];
NSString *str=@"我爱你!";
NSString *strPath=[documents[0] stringByAppendingPathComponent:@"1.txt"];
[str writeToFile:strPath atomically:YES encoding:NSUTF8StringEncoding error:nil];
//关闭图片选择控制器
[picker dismissViewControllerAnimated:YES completion:nil];
NSLog(@"%@",path);
}
@end