码迷,mamicode.com
首页 > 移动开发 > 详细

iOS 图片的存储以及读取和删除

时间:2016-08-23 09:04:04      阅读:219      评论:0      收藏:0      [点我收藏+]

标签:

将图片存储到本地

 NSArray *dirArray = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
            NSString *path = [dirArray firstObject];
            path = [path stringByAppendingPathComponent:[NSString stringWithFormat:@"IDCARD%zd",_selectIndex]];
            NSData *imageData = UIImageJPEGRepresentation(image, 1.0);
            [imageData writeToFile:path atomically:YES];

图片的存到本地之后的读取

 NSArray *dirArray = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
        NSString *path = [dirArray firstObject];
        path = [path stringByAppendingPathComponent:[NSString stringWithFormat:@"IDCARD%zd",i]];
        if([[NSFileManager defaultManager] fileExistsAtPath:path]){
            NSData *picData = [NSData dataWithContentsOfFile:path];
            view.idCardImageView.image = [UIImage imageWithData:picData];
            }

将存储到本地的图片删除

 NSArray *dirArray = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
        NSString *path = [dirArray firstObject];
        path = [path stringByAppendingPathComponent:[NSString stringWithFormat:@"IDCARD%zd",i]];
        if([[NSFileManager defaultManager] fileExistsAtPath:path])//如果存在临时文件的配置文件
        {
            [[NSFileManager defaultManager]  removeItemAtPath:path error:nil];
        }

iOS 图片的存储以及读取和删除

标签:

原文地址:http://www.cnblogs.com/carlos-mm/p/5797925.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!