iOS保存图片到document

首先写一个保存的方法:

-(NSString *)savescanresultimage:(UIImage *)resultimage imagename:(NSString *)strimagename
{
    NSData *imageData = UIImagePNGRepresentation(resultimage);
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *filePath = [documentsDirectory stringByAppendingPathComponent:strimagename]; //Add the file name
    [imageData writeToFile:filePath atomically:YES];
    return filePath;
}

然后再你想保存的方法中加上下面的代码:


//给图片以日期命名
NSDateFormatter * fmt = [[NSDateFormatter alloc] init] ;
[fmt setDateFormat:@"yyyy/MM/dd"];
NSDateFormatter * fmthsm = [[NSDateFormatter alloc] init] ;
[fmthsm setDateFormat:@"yyyyMMddHHmmss"];
NSString * imagename = [[NSString alloc]initWithFormat:@"%@.png",[fmthsm stringFromDate:[NSDate date]]];
//调用方法保存图片
NSString * filePathString = [self savescanresultimage:保存的图片 imagename:imagename];

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 174,581评论 25 709
  • 1. Java基础部分 基础部分的顺序:基本语法,类相关的语法,内部类的语法,继承相关的语法,异常的语法,线程的语...
    子非鱼_t_阅读 31,839评论 18 399
  • 到杭州快一个月了,每天穿梭于宿舍和上班的地方。还好,住宿的地方离西湖只有1公里多而上班的地方就靠近钱塘江边,所以难...
    九尾貂阅读 441评论 4 1
  • 第七章 社区商务活动方式的兴起 第二节 商业流通体系的改变 1.商家市场地位的改变 2.重塑厂商关系的必要 1.商...
    夜风微晨阅读 751评论 0 1