视频为例如下:
//导入
#include <CommonCrypto/CommonDigest.h>
/// 获取视频MD5方法
/// @param url 视频url,暂未用到
/// @param localAsset 本地的asset数据
- (void)getPhoneDateVideo:(NSURL *)url phAsset:(PHAsset *)localAsset
{
PHVideoRequestOptions *options = [[PHVideoRequestOptions alloc] init];
options.version = PHImageRequestOptionsVersionCurrent;
options.deliveryMode = PHVideoRequestOptionsDeliveryModeAutomatic;
PHImageManager *manager = [PHImageManager defaultManager];
[manager requestAVAssetForVideo:localAsset
options:options
resultHandler:^(AVAsset * _Nullable asset, AVAudioMix * _Nullable audioMix, NSDictionary * _Nullable info) {
// asset 类型为 AVURLAsset 为此资源的fileURL
// <AVURLAsset: 0x283386e60, URL = file:///var/mobile/Media/DCIM/100APPLE/IMG_0049.MOV>
AVURLAsset *urlAsset = (AVURLAsset *)asset;
// 视频数据
NSData *vedioData = [NSData dataWithContentsOfURL:urlAsset.URL];
NSLog(@"%@",vedioData);
[vedioData writeToFile:KVideoUrlPath atomically:YES];
dispatch_async(dispatch_get_main_queue(), ^{
//获取视频MD5
NSData *data = vedioData;
unsigned char digest[CC_MD5_DIGEST_LENGTH];
CC_MD5( data.bytes, (CC_LONG)data.length, digest );
NSMutableString *output = [NSMutableString stringWithCapacity:CC_MD5_DIGEST_LENGTH * 2];
for( int i = 0; i < CC_MD5_DIGEST_LENGTH; i++ )
{
[output appendFormat:@"%02x", digest[i]];
}
NSLog(@"视频的MD5:%@",output);
});
}];
}