2016-04-01 9 views
0

Ich entwickle eine App, die UICollectionView wie # zhangao0086/DKImagePickerController # Beispiel in Github hat. Jetzt muss ich die angezeigten UICollectionviewCell Bilder auf den Server hochladen. Kann mir jemand die richtigen Tuts zum Hochladen vorschlagen? Danke im Voraus.hochladen Sammlung Bilder in Server mit swift

UICollectionView Code wie folgt:

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell { 
    let asset = self.assets![indexPath.row] 
    var cell: UICollectionViewCell? 
    var imageView: UIImageView? 

    if asset.isVideo { 
     cell = collectionView.dequeueReusableCellWithReuseIdentifier("CellVideo", forIndexPath: indexPath) 
     imageView = cell?.contentView.viewWithTag(1) as? UIImageView 
    } else { 
     cell = collectionView.dequeueReusableCellWithReuseIdentifier("CellImage", forIndexPath: indexPath) 
     imageView = cell?.contentView.viewWithTag(1) as? UIImageView 
    } 

    if let cell = cell, imageView = imageView { 
     let layout = collectionView.collectionViewLayout as! UICollectionViewFlowLayout 
     let tag = indexPath.row + 1 
     cell.tag = tag 
     asset.fetchImageWithSize(layout.itemSize.toPixel(), completeBlock: { image, info in 
      if cell.tag == tag { 
       imageView.image = image 
      } 
     }) 
    } 

    return cell! 
} 

das Bild in Server

func barButtonItemClicked(barButtonItem: UIBarButtonItem) 
{ 
    let myUrl = NSURL(string: "http://moneymonkey.tokiiyo.com/api/signature"); 

    let typeItem: InsuranceType = InsuranceManager.sharedInstance.TypeArray[0] 
    let compItem: Companies = InsuranceManager.sharedInstance.CompArray[0] 

    let request = NSMutableURLRequest(URL:myUrl!); 
    request.HTTPMethod = "POST"; 

    let param = [ 
     "api_key" : "AiK58j67", 
     "api_secret" : "a#9rJkmbOea90-", 
     "phone" : "\(mobile)", 
     "policy_type" : "\(typeItem.name)", 
     "company" : "\(compItem.cname)" 
    ] 

    print("Policy_type: \(typeItem.name)") 
    let boundary = generateBoundaryString() 

    request.setValue("multipart/form-data; boundary=\(boundary)", forHTTPHeaderField: "Content-Type") 

    let imageData = UIImagePNGRepresentation(?) //here what imageView 

    if(imageData==nil) { return; } 

    request.HTTPBody = createBodyWithParameters(param, filePathKey: "file", imageDataKey: imageData!, boundary: boundary) 


    let task = NSURLSession.sharedSession().dataTaskWithRequest(request) { 
     data, response, error in 

     if error != nil { 
      print("error=\(error)") 
      return 
     } 

     // You can print out response object 
     print("******* response = \(response)") 

     // Print out reponse body 
     let responseString = NSString(data: data!, encoding: NSUTF8StringEncoding) 
     print("****** response data = \(responseString!)") 
     do{ 

      _ = try NSJSONSerialization.JSONObjectWithData(data!, options: .MutableContainers) as? NSDictionary 
      dispatch_async(dispatch_get_main_queue(),{ 
      }); 
     } 
     catch 
     { 

      // report error 
      print("Oops!! Something went wrong\(error)") 
     } 
    } 

    task.resume() 

} 

func createBodyWithParameters(parameters: [String: String]?, filePathKey: String?, imageDataKey: NSData, boundary: String) -> NSData { 
    let body = NSMutableData(); 

    if parameters != nil { 
     for (key, value) in parameters! { 
      body.appendString("--\(boundary)\r\n") 
      body.appendString("Content-Disposition: form-data; name=\"\(key)\"\r\n\r\n") 
      body.appendString("\(value)\r\n") 
     } 
    } 

    let filename = "image.png" 

    let mimetype = "image/png" 

    body.appendString("--\(boundary)\r\n") 
    body.appendString("Content-Disposition: form-data; name=\"\(filePathKey!)\"; filename=\"\(filename)\"\r\n") 
    body.appendString("Content-Type: \(mimetype)\r\n\r\n") 
    body.appendData(imageDataKey) 
    body.appendString("\r\n") 



    body.appendString("--\(boundary)--\r\n") 

    return body 
} 

func generateBoundaryString() -> String { 
    return "Boundary-\(NSUUID().UUIDString)" 
} 

Antwort

0

J

Upload können Sie Alamofire verwenden https://github.com/Alamofire/Alamofire

Verwendung wie folgt aus:

Alamofire.upload(.POST, "YourURl", file: YourFile) 
     .progress { bytesWritten, totalBytesWritten, totalBytesExpectedToWrite in 
      print(totalBytesWritten) 

      // This closure is NOT called on the main queue for performance 
      // reasons. To update your ui, dispatch to the main queue. 
      dispatch_async(dispatch_get_main_queue()) { 
       print("Total bytes written on main queue: \(totalBytesWritten)") 
      } 
     } 
     .responseJSON { response in 
      debugPrint(response) 
     }