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

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