칠우 서버(사진을 칠우에 업로드하고 사이트 주소 반환)

2293 단어
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)
                
            }
        }
    }
}

좋은 웹페이지 즐겨찾기