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];

你可能感兴趣的:(iOS保存图片到document)