Ios 计算视频文件md5(散列)

Ios 计算视频文件md5(散列),ios,objective-c,swift,swift3,Ios,Objective C,Swift,Swift3,我必须计算所选文件(图像、视频)的校验和 在我的代码中,图像计算非常有效,但视频计算根本不起作用 我正在使用加密软件 这是我的代码我做了什么 import CryptoSwift class MD5Calculator { static func imageChecksum(imageArray: [UIImage], onCalculated: @escaping ([String]) -> Void){ DispatchQueue.global(qos:

我必须计算所选文件(图像、视频)的校验和

在我的代码中,图像计算非常有效,但视频计算根本不起作用

我正在使用加密软件

这是我的代码我做了什么

import CryptoSwift

class MD5Calculator {

    static func imageChecksum(imageArray: [UIImage], onCalculated: @escaping ([String]) -> Void){

        DispatchQueue.global(qos: .userInitiated).async {

            var array: [String] = []                        
            for chosenImage in imageArray {
                if let jpegData = UIImageJPEGRepresentation(chosenImage, 80) {
                    let checksum = jpegData.md5()
                    let chsum = checksum.toHexString()
                    array.append(chsum)
                }
            }

            DispatchQueue.main.async {
                onCalculated(array)
            }
        }
    }

    static func videoChecksum(videoURLs:[NSURL], onCalculated: @escaping ([String]) -> Void) {
        DispatchQueue.global(qos: .userInitiated).async {

            var array: [String] = []

            for url in videoURLs {
                if let videoData = Data(contentsOf: url as URL, options: Data.ReadingOptions) {
                    let checksum = videoData.md5()
                    let chsum = checksum.toHexString()
                    array.append(chsum)

                }
            }

            DispatchQueue.main.async {
                onCalculated(array)
            }
        }
    }
}
在videoChecksum中,我无法获取数据,因为行中有语法错误

 if let videoData = Data(contentsOf: url as URL, options: Data.ReadingOptions) 
错误是:

Cannot convert value of type 'Data.ReadingOptions.Type' (aka 'NSData.ReadingOptions.Type') to expected argument type 'Data.ReadingOptions' (aka 'NSData.ReadingOptions')
这是数据类构造函数

public init(contentsOf url: URL, options: Data.ReadingOptions = default) throws
我的问题是

1。如何获取视频文件的数据

2。如果有其他方法获取数据并计算校验和,请建议我


3。如果您知道这是什么语法错误,请告诉我如何修复。

打电话时似乎出现语法错误

let videoData = Data(contentsOf: url as URL, options: Data.ReadingOptions)
因为
options
参数需要一个
ReadingOptions
值,而您正在传递类型本身。有效呼叫如下所示:

let videoData = Data(contentsOf: url as URL, options: Data.ReadingOptions.uncached)
for url in videoURLs {
    do {
        let videoData = try Data(contentsOf: url as URL)
        let checksum = videoData.md5()
        let chsum = checksum.toHexString()
        array.append(chsum)
    } catch {
        // TODO: Handle error
        print(error.localizedDescription)
    }
}
(有关可能的值,请参见。)

如果不确定要提供什么值,可以忽略传递默认参数,即:

let videoData = Data(contentsOf: url as URL)

编辑:
还要注意,
数据(contentsOf:options:)
初始化器“
抛出
”,因此理想情况下,您应该将调用封装在do/try/catch语句中,如下所示:

let videoData = Data(contentsOf: url as URL, options: Data.ReadingOptions.uncached)
for url in videoURLs {
    do {
        let videoData = try Data(contentsOf: url as URL)
        let checksum = videoData.md5()
        let chsum = checksum.toHexString()
        array.append(chsum)
    } catch {
        // TODO: Handle error
        print(error.localizedDescription)
    }
}
希望这有帮助。

试试这个

ALAssetRepresentation*rep=[asset defaultRepresentation]; 字节缓冲区=(字节)malloc((整数)rep.size); NSInteger buffered=[rep getBytes:buffer fromOffset:0.0 length:(NSInteger)rep.size错误:nil]; NSData*data=[NSData data WITHBYTESNOPY:缓冲区长度:缓冲区空闲时间当域:是]

很高兴听到:)也可以看到我对异常处理的编辑。