在網(wǎng)上查了好多方法復(fù)制過來都有問題包蓝,最后找了這個是最準(zhǔn)確的彪置,幫大家減少走彎路泡垃。
ADUtilHelper.h
//獲取視頻MD5
#import <Foundation/Foundation.h>
#import <SystemConfiguration/CaptiveNetwork.h>
@interface ADUtilHelper : NSObject
+(NSString *)getFileMD5WithPath:(NSString*)path;
+(NSString *)getWifiName;
@end
ADUtilHelper.m
#define FileHashDefaultChunkSizeForReadingData 1024*8
#import "ADUtilHelper.h"
#include <CommonCrypto/CommonDigest.h>
@implementation ADUtilHelper
+(NSString*)getFileMD5WithPath:(NSString*)path
{
return (__bridge_transfer NSString *)FileMD5HashCreateWithPath((__bridge CFStringRef)path, FileHashDefaultChunkSizeForReadingData);
}
CFStringRef FileMD5HashCreateWithPath(CFStringRef filePath,size_t chunkSizeForReadingData) {
// Declare needed variables
CFStringRef result = NULL;
CFReadStreamRef readStream = NULL;
// Get the file URL
CFURLRef fileURL =
CFURLCreateWithFileSystemPath(kCFAllocatorDefault,
(CFStringRef)filePath,
kCFURLPOSIXPathStyle,
(Boolean)false);
if (!fileURL) goto done;
// Create and open the read stream
readStream = CFReadStreamCreateWithFile(kCFAllocatorDefault,
(CFURLRef)fileURL);
if (!readStream) goto done;
bool didSucceed = (bool)CFReadStreamOpen(readStream);
if (!didSucceed) goto done;
// Initialize the hash object
CC_MD5_CTX hashObject;
CC_MD5_Init(&hashObject);
// Make sure chunkSizeForReadingData is valid
if (!chunkSizeForReadingData) {
chunkSizeForReadingData = FileHashDefaultChunkSizeForReadingData;
}
// Feed the data to the hash object
bool hasMoreData = true;
while (hasMoreData) {
uint8_t buffer[chunkSizeForReadingData];
CFIndex readBytesCount = CFReadStreamRead(readStream,(UInt8 *)buffer,(CFIndex)sizeof(buffer));
if (readBytesCount == -1) break;
if (readBytesCount == 0) {
hasMoreData = false;
continue;
}
CC_MD5_Update(&hashObject,(const void *)buffer,(CC_LONG)readBytesCount);
}
// Check if the read operation succeeded
didSucceed = !hasMoreData;
// Compute the hash digest
unsigned char digest[CC_MD5_DIGEST_LENGTH];
CC_MD5_Final(digest, &hashObject);
// Abort if the read operation failed
if (!didSucceed) goto done;
// Compute the string result
char hash[2 * sizeof(digest) + 1];
for (size_t i = 0; i < sizeof(digest); ++i) {
snprintf(hash + (2 * i), 3, "%02x", (int)(digest[i]));
}
result = CFStringCreateWithCString(kCFAllocatorDefault,(const char *)hash,kCFStringEncodingUTF8);
done:
if (readStream) {
CFReadStreamClose(readStream);
CFRelease(readStream);
}
if (fileURL) {
CFRelease(fileURL);
}
return result;
}
+(NSString *)getWifiName{
NSString *wifiName = @"";
CFArrayRef wifiInterfaces = CNCopySupportedInterfaces();
if (!wifiInterfaces) {
return nil;
}
NSArray *interfaces = (__bridge NSArray *)wifiInterfaces;
for (NSString *interfaceName in interfaces) {
CFDictionaryRef dictRef = CNCopyCurrentNetworkInfo((__bridge CFStringRef)(interfaceName));
if (dictRef) {
NSDictionary *networkInfo = (__bridge NSDictionary *)dictRef;
NSLog(@"network info -> %@", networkInfo);
wifiName = [networkInfo objectForKey:(__bridge NSString *)kCNNetworkInfoKeySSID];
NSLog(@"wifiName----%@",wifiName);
CFRelease(dictRef);
}
}
CFRelease(wifiInterfaces);
return wifiName;
}
@end
補(bǔ)充:相冊視頻獲取url并把視頻文件存入沙盒
獲取相冊視頻
//代理
UIImagePickerControllerDelegate,UINavigationControllerDelegate
//從手機(jī)相冊選取視頻
UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
imagePicker.delegate = self;
//視頻編輯
imagePicker.allowsEditing = YES;
//相冊選取==UIImagePickerControllerSourceTypeSavedPhotosAlbum(打開所有視頻努释,而不是列表)
imagePicker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
//設(shè)置選取類型苫耸,只能是視頻
imagePicker.mediaTypes = [[NSArray alloc] initWithObjects: (NSString *) kUTTypeMovie, nil];
[self presentViewController:imagePicker animated:YES completion:nil];
#pragma mark -
#pragma UIImagePickerController Delegate
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
if ([[info objectForKey:UIImagePickerControllerMediaType] isEqualToString:(NSString*)kUTTypeImage]) {
//獲取到圖片
// UIImage *img = [info objectForKey:UIImagePickerControllerEditedImage];
// self.fileData = UIImageJPEGRepresentation(img, 1.0);
} else if ([[info objectForKey:UIImagePickerControllerMediaType] isEqualToString:(NSString*)kUTTypeMovie]) {
//獲取到視頻路徑(有些視頻沒有這個值,所以不用他)
// NSString *videoPath1 = [[info objectForKey:UIImagePickerControllerMediaURL] path];
//獲取視頻的url(所有視頻都有這個值)
NSURL * videoXT_URL = [info objectForKey:UIImagePickerControllerReferenceURL];
[picker dismissViewControllerAnimated:YES completion:nil];
}
}
存入沙盒
//系統(tǒng)視頻上傳存儲沙盒路徑(再用)臨時(shí)緩存:NSCachesDirectory
#define KVideoUrlPath \
[[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) objectAtIndex:0] stringByAppendingPathComponent:@"XiumeiVideoURLDWB"]
// 將原始視頻的URL轉(zhuǎn)化為NSData數(shù)據(jù),寫入沙盒
- (void)getPhoneDateVideo:(NSURL *)url
{
// 解析一下,為什么視頻不像圖片一樣一次性開辟本身大小的內(nèi)存寫入?
// 想想,如果1個視頻有1G多,難道直接開辟1G多的空間大小來寫?
// 創(chuàng)建存放原始圖的文件夾--->VideoURL
NSFileManager * fileManager = [NSFileManager defaultManager];
if (![fileManager fileExistsAtPath:KVideoUrlPath]) {
[fileManager createDirectoryAtPath:KVideoUrlPath withIntermediateDirectories:YES attributes:nil error:nil];
}
ALAssetsLibrary *assetLibrary = [[ALAssetsLibrary alloc] init];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
if (url) {
[assetLibrary assetForURL:url resultBlock:^(ALAsset *asset) {
ALAssetRepresentation *rep = [asset defaultRepresentation];
//路徑后面拼接當(dāng)前日期做區(qū)分贸人,否則會讀取成一個文件
// NSString * dateString = [NSString getNowDateFormat:@"yyyyMMddHHmmss"];
//視頻名字
NSString * dateString = rep.filename;
// NSLog(@"%@",dateString);
NSString * videoPath = [KVideoUrlPath stringByAppendingPathComponent:dateString];
const char *cvideoPath = [videoPath UTF8String];
FILE *file = fopen(cvideoPath, "a+");
if (file) {
const int bufferSize = 11024 * 1024;
// 初始化一個1M的buffer
Byte *buffer = (Byte*)malloc(bufferSize);
NSUInteger read = 0, offset = 0, written = 0;
NSError* err = nil;
if (rep.size != 0)
{
do {
read = [rep getBytes:buffer fromOffset:offset length:bufferSize error:&err];
written = fwrite(buffer, sizeof(char), read, file);
offset += read;
} while (read != 0 && !err);//沒到結(jié)尾间景,沒出錯,ok繼續(xù)
}
// 釋放緩沖區(qū)艺智,關(guān)閉文件
free(buffer);
buffer = NULL;
fclose(file);
file = NULL;
// UI的更新記得放在主線程,要不然等子線程排隊(duì)過來都不知道什么年代了,會很慢的
dispatch_async(dispatch_get_main_queue(), ^{
//獲取視頻MD5
self.videoStringMD5 = [ADUtilHelper getFileMD5WithPath:videoPath];
// NSLog(@"視頻的MD5:%@",self.videoStringMD5);
});
}else{
dispatch_async(dispatch_get_main_queue(), ^{
[DWBToast showCenterWithText:@"視頻處理失敗"];
});
}
} failureBlock:^(NSError *error) {
dispatch_async(dispatch_get_main_queue(), ^{
[DWBToast showCenterWithText:@"視頻處理失敗"];
});
}];
}
});
}