将md5的照片转化成json字符串

/**
 *  将md5的照片转化成json字符串
 *
 *  @param checksumArray 装有照片MD5的数组
 *
 *  @return json字符串
 */
- (NSString *)markChecksum:(NSArray *)checksumArray
{
    if (!checksumArray || checksumArray.count == 0) {
        return @"";
    }
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:checksumArray options:0 error:nil];
    if (!jsonData) {
        return @"";
    }
    NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
    return jsonString;
}

/**
 *  将图片data写入磁盘用于以后压缩
 *
 *  @param imageData 图片的data
 */
- (void)writeImageData2File:(NSData *)imageData
{
    if (imageData == nil) {
        return;
    }
    //文件保存路径
    NSString *directoryPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
    NSString *filePath = [directoryPath stringByAppendingPathComponent:@"Nearby/mark/photo"];
    
    if (![[NSFileManager defaultManager] fileExistsAtPath:filePath]) {
        [[NSFileManager defaultManager] createDirectoryAtPath:filePath withIntermediateDirectories:YES attributes:nil error:nil];
    }
    NSString *imageMD5 = [self MD5StringWithData:imageData];
    NSString *photoPath = [NSString stringWithFormat:@"%@/%@", filePath, [NSString stringWithFormat:@"%@.jpg", imageMD5]];
    if ([[NSFileManager defaultManager] fileExistsAtPath:photoPath]) {
        //文件重复,无效.
        [[NSFileManager defaultManager] removeItemAtPath:photoPath error:nil];
    }
    if( [imageData writeToFile:photoPath atomically:YES]) {
        if (imageMD5 && [imageMD5 length]) {
            [_photoMD5Array addObject:imageMD5];
        } else {
            [_photoMD5Array addObject:@""];
        }
        [_photoPathArray addObject:photoPath];
    }
}

/**
 *  获取图片data的md5值
 *
 *  @param data 压缩后的图片data
 *
 *  @return 图片的md5值
 */
- (NSString *)MD5StringWithData:(NSData *)data
{
    const unsigned char *signBytes = [data bytes];
    unsigned char outputBuffer[CC_MD5_DIGEST_LENGTH];
    CC_MD5(signBytes, (int)data.length, outputBuffer);
    
    NSMutableString *outputString = [[NSMutableString alloc] initWithCapacity:CC_MD5_DIGEST_LENGTH * 2];
    for (NSInteger count = 0; count < CC_MD5_DIGEST_LENGTH; count++) {
        [outputString appendFormat:@"%02X",outputBuffer[count]];
    }
    return outputString ;
}

/**
 *  将上传的图片压缩成一个压缩包
 *
 *  @return 返回压缩包路径
 */
- (NSString *)zipPhotoFile:(NSArray *)photoPathArray
{
    //文件保存路径
    NSString *directoryPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
    NSString *filePath = [directoryPath stringByAppendingPathComponent:@"Nearby/mark"];
    if (![[NSFileManager defaultManager] fileExistsAtPath:filePath]) {
        [[NSFileManager defaultManager] createDirectoryAtPath:filePath withIntermediateDirectories:YES attributes:nil error:nil];
    }
    NSString *zipFile = [filePath stringByAppendingPathComponent:@"newzipfile.zip"];
    if ([[NSFileManager defaultManager] fileExistsAtPath:zipFile]) {
        [[NSFileManager defaultManager] removeItemAtPath:zipFile error:NULL];
    }
    ZipArchive *za = [[ZipArchive alloc] init];
    [za CreateZipFile2:zipFile];
    for (NSString *path in photoPathArray) {
        [za addFileToZip:path newname:[path lastPathComponent]];
    }
    if ([za CloseZipFile2]) {
        return zipFile;
    }
    return nil;
}

你可能感兴趣的:(将md5的照片转化成json字符串)