七牛服务器(将图片上传到七牛并返还图片网址)

class QiniuUPDownData: NSObject {
    
    static func upData(imageData: NSData!, closure: (url:NSURL) -> Void) {
        //设置七牛管理器
        let upManager = QNUploadManager()
        //设置随机数
        let randomNum = arc4random_uniform(99999999)
        //获取当前时间
        let date = NSDate()
        //自定义日期格式  网址: nadateformatter.com
        let timeformatter = NSDateFormatter()
        timeformatter.dateFormat = "yyyy-MM-dd"
        //将日期转换成字符串格式
        let strTime = timeformatter.stringFromDate(date)
        //设置图片名格式
        let imageName = strTime + "-\(randomNum).jpeg"
        let opt = QNUploadOption(mime: "x:image/jpeg", progressHandler: nil, params: ["1": "pic1"], checkCrc: false, cancellationSignal: nil)
        //获取token
        Alamofire.request(.POST, NSURL(string: "http://api.diveinedu.com/carunion/index.php?s=/home/Qiniu/getUploadToken")!, parameters: nil, encoding: .URL, headers: nil).responseJSON { (response) in
            let arr = JSON(data: response.data!)
            print(arr["info"].string!)
            if arr["status"].bool! {
                print(arr["token"])
                let token = arr["token"].string!
                upManager.putData(imageData, key: imageName, token: token, complete: { (responseInfo, strKey, anyObjects) in
                        if let info = responseInfo {
                            print(info)
                            if info.statusCode == 200 {
                                print( "http://oci97e7ke.bkt.clouddn.com/" + imageName)
                                //获得图片网址
                                let url = NSURL(string: "http://oci97e7ke.bkt.clouddn.com/" + imageName)
                                closure(url:url!)
                            }
                            else {
                                print("NO info")
                            }
                            if let key = strKey {
                                print(key)
                            }
                            else {
                                print("NO key")
                            }
                        }
                    }, option: opt)
                
            }
        }
    }
}

你可能感兴趣的:(七牛服务器(将图片上传到七牛并返还图片网址))