因为项目要使用到七牛云保存资源文件,所以就自己整理了一个七牛云的上传工具类方便以后的开发和使用,希望能帮到大家!
let kQiniuBucket = "你项目的Bucket"
let kQiniuAccessKey = "你项目的AccessKey"
let kQiniuSecretKey = "你项目的SecretKey "
import UIKit
class QiniuTools {
static var filePath = String()
static var sharedInstance = QiniuTools()
var Index : Int = 0
init() {
}
func token() -> String {
return self.createQiniuToken(fileName: kQiniuBucket)
}
func hmacsha1WithString(str: String, secretKey: String) -> NSData {
let cKey = secretKey.cString(using: String.Encoding.ascii)
let cData = str.cString(using: String.Encoding.ascii)
var result = [CUnsignedChar](repeating: 0, count: Int(CC_SHA1_DIGEST_LENGTH))
CCHmac(CCHmacAlgorithm(kCCHmacAlgSHA1), cKey!, Int(strlen(cKey!)), cData!, Int(strlen(cData!)), &result)
let hmacData: NSData = NSData(bytes: result, length: (Int(CC_SHA1_DIGEST_LENGTH)))
return hmacData
}
func createQiniuToken(fileName: String) -> String {
let oneHourLater = NSDate().timeIntervalSince1970 + 3600
let putPolicy: NSDictionary = ["scope": kQiniuBucket, "deadline": NSNumber(value: UInt64(oneHourLater))]
let encodedPutPolicy = QNUrlSafeBase64.encode(putPolicy.jsonString())
let sign = self.hmacsha1WithString(str: encodedPutPolicy!, secretKey: kQiniuSecretKey)
let encodedSign = QNUrlSafeBase64.encode(sign as Data!)
return kQiniuAccessKey + ":" + encodedSign! + ":" + encodedPutPolicy!
}
func uploadImageData(image:Data , result: @escaping (_ progress: Float? , _ imageKey:String? ) -> ()) {
let token = self.token()
let opt = QNUploadOption(mime: nil, progressHandler: {(key, progres) in
result(progres, nil)
}, params: nil, checkCrc: true, cancellationSignal: nil)
var cutdownData : Data!
if (image.count < 9999) {
cutdownData = image
} else if (image.count < 99999) {
let nowImage = UIImage.init(data: image)!
cutdownData = UIImageJPEGRepresentation(nowImage, 0.6)
} else {
let nowImage = UIImage.init(data: image)!
cutdownData = UIImageJPEGRepresentation(nowImage, 0.3)
}
if let manager = QNUploadManager() {
manager.put(cutdownData, key: nil, token: token, complete: { (Info, key, resp) in
if (Info?.isConnectionBroken)! {
NOAlertManager.showErrorHUD("网络连接错误")
return
}
if let imageKey = resp?["key"] as? String {
result(nil, imageKey)
}
}, option: opt)
}
}
func uploadVideoData(video:Data , result: @escaping (_ progress: Float? , _ imageKey:String? ) -> ()) {
let token = self.token()
let opt = QNUploadOption(mime: nil, progressHandler: {(key, progres) in
result(progres, nil)
}, params: nil, checkCrc: true, cancellationSignal: nil)
if let manager = QNUploadManager() {
manager.put(video, key: nil, token: token, complete: { (Info, key, resp) in
if (Info?.isConnectionBroken)! {
NOAlertManager.showErrorHUD("网络连接错误")
return
}
if let imageKey = resp?["key"] as? String {
result(nil, imageKey)
}
}, option: opt)
}
}
func upVideoDatas(videos:[Data] , result: @escaping (_ progress: Float? , _ imageKey:String? ) -> (),allTasksCompletion:@escaping () -> () ) {
if (Index < videos.count) {
uploadVideoData(video: videos[Index], result: { (progres, imageKey) in
if (imageKey != nil) {
result(progres, imageKey)
self.Index += 1
self.upVideoDatas(videos: videos, result: result, allTasksCompletion: allTasksCompletion)
}
})
}else{
allTasksCompletion()
Index = 0
}
}
func upImageDatas(images:[Data] , result: @escaping (_ progress: Float? , _ imageKey:String? ) -> (),allTasksCompletion:@escaping () -> () ) {
if (Index < images.count) {
uploadImageData(image: images[Index], result: { (progres, imageKey) in
if (imageKey != nil) {
result(progres, imageKey)
self.Index += 1
self.upImageDatas(images: images, result: result, allTasksCompletion: allTasksCompletion)
}
})
}else{
allTasksCompletion()
Index = 0
}
}
}