美文网首页ios
iOS保存图片到document

iOS保存图片到document

作者: jay_den | 来源:发表于2017-03-13 09:32 被阅读549次

    首先写一个保存的方法:

    -(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

        本文链接:https://www.haomeiwen.com/subject/vzkfnttx.html