UIimage存入本地并读取
2015-11-24 18:26
211 查看
#import "QFViewController.h"
@interface QFViewController ()
@end
@implementation QFViewController
@synthesize imageView;
@synthesize image;
@synthesize dic;
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
UIButton *button = [[UIButton alloc] init];
[button setFrame:CGRectMake(0, 0, 320, 60)];
[button setTitle:@"获取图片" forState:UIControlStateNormal];
[button setBackgroundColor:[UIColor yellowColor]];
[button addTarget:nil action:@selector(btnImage:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 80
, 320, 400)];
imageView.backgroundColor = [UIColor blueColor];
[self.view addSubview:imageView];
[self plist];
}
-(void)btnImage:(id)sender
{
UIActionSheet *sheet = [[UIActionSheet alloc] initWithTitle:@"选择" delegate:self cancelButtonTitle:nil destructiveButtonTitle:@"取消" otherButtonTitles:@"拍照",@"从相册选择", nil];
[sheet showInView:self.view];
}
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex==1) {
UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
imagePickerController.delegate = self;
imagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera;
[self presentViewController:imagePickerController animated:YES completion:^{}];
}else if (buttonIndex==2){
UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
imagePickerController.delegate = self;
imagePickerController.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
[self presentViewController:imagePickerController animated:YES completion:^{}];
}
}
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
[picker dismissViewControllerAnimated:YES completion:nil];
image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
[imageView setImage:image];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
NSString *filePath = [[paths objectAtIndex:0] stringByAppendingPathComponent:[NSString stringWithFormat:@"pic_100.png"]]; // 保存文件的名称
[UIImagePNGRepresentation(image)writeToFile: filePath atomically:YES];
[self dicPaths
];
}
-(void)dicPaths
{
NSMutableArray *specialArr = [[NSMutableArray alloc] initWithCapacity:0];
dic = [[NSMutableDictionary alloc]init];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
NSString *filePath = [[paths objectAtIndex:0] stringByAppendingPathComponent:[NSString stringWithFormat:@"pic_100.png"]]; // 保存文件的名称
[dic setObject:filePath forKey:@"img"];
[specialArr addObject:dic];
}
-(void)plist
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
NSString *filePath = [[paths objectAtIndex:0] stringByAppendingPathComponent:[NSString stringWithFormat:@"pic_100.png"]]; // 保存文件的名称
UIImage *img = [UIImage imageWithContentsOfFile:filePath];
[imageView setImage:img];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
@interface QFViewController ()
@end
@implementation QFViewController
@synthesize imageView;
@synthesize image;
@synthesize dic;
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
UIButton *button = [[UIButton alloc] init];
[button setFrame:CGRectMake(0, 0, 320, 60)];
[button setTitle:@"获取图片" forState:UIControlStateNormal];
[button setBackgroundColor:[UIColor yellowColor]];
[button addTarget:nil action:@selector(btnImage:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 80
, 320, 400)];
imageView.backgroundColor = [UIColor blueColor];
[self.view addSubview:imageView];
[self plist];
}
-(void)btnImage:(id)sender
{
UIActionSheet *sheet = [[UIActionSheet alloc] initWithTitle:@"选择" delegate:self cancelButtonTitle:nil destructiveButtonTitle:@"取消" otherButtonTitles:@"拍照",@"从相册选择", nil];
[sheet showInView:self.view];
}
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex==1) {
UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
imagePickerController.delegate = self;
imagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera;
[self presentViewController:imagePickerController animated:YES completion:^{}];
}else if (buttonIndex==2){
UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
imagePickerController.delegate = self;
imagePickerController.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
[self presentViewController:imagePickerController animated:YES completion:^{}];
}
}
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
[picker dismissViewControllerAnimated:YES completion:nil];
image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
[imageView setImage:image];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
NSString *filePath = [[paths objectAtIndex:0] stringByAppendingPathComponent:[NSString stringWithFormat:@"pic_100.png"]]; // 保存文件的名称
[UIImagePNGRepresentation(image)writeToFile: filePath atomically:YES];
[self dicPaths
];
}
-(void)dicPaths
{
NSMutableArray *specialArr = [[NSMutableArray alloc] initWithCapacity:0];
dic = [[NSMutableDictionary alloc]init];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
NSString *filePath = [[paths objectAtIndex:0] stringByAppendingPathComponent:[NSString stringWithFormat:@"pic_100.png"]]; // 保存文件的名称
[dic setObject:filePath forKey:@"img"];
[specialArr addObject:dic];
}
-(void)plist
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
NSString *filePath = [[paths objectAtIndex:0] stringByAppendingPathComponent:[NSString stringWithFormat:@"pic_100.png"]]; // 保存文件的名称
UIImage *img = [UIImage imageWithContentsOfFile:filePath];
[imageView setImage:img];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
相关文章推荐
- UIPickerView(API+选择器使用)
- 百度编辑器UEditor使用
- easyui-datagrid 编辑模式详解
- POJ 2299 Ultra-QuickSort 【树状数组求逆序数】
- html required属性
- UIView 设置圆角和边框备忘
- Apue Chapter 6 习题答案
- easyui combobox 随便输入,清除不存在输入值
- iOS 官方文档 UIView
- gradle.tooling.ModelBuilderService not found
- HDU 4339 Query【线段树】单点更新,动态查询
- NSOperationQueue XXXX isFinished=YES without being started by the queue it is in
- iOS 动画 UIView动画
- M6: 使用摄像头(CameraCaptureUI)
- 网站开发进阶(二十二)HTML UI知识汇总(更新中...)
- 网站开发进阶(二十二)HTML UI知识汇总(更新中...)
- UIView的layoutSubviews和drawRect方法何时调用
- ios UITableView修改右侧滚动条的颜色
- UITextView - 2
- iOS 使用AFNetworking遇到错误 Request failed: unacceptable content-type: text/html