Я сделал свой проект с загрузкой изображений на сервер, используя скрипт php.Он хорошо работает на Swift 2 с alomafire.
После обновления до swift 4 с alomafire4 коды загрузки не работают.
@IBAction func create(_ sender: AnyObject) {
showLoadingMode (loading: self)
let name = grpnametf.text
let defaults = UserDefaults.standard
let member_id = defaults.object(forKey: "id") as! String
let country = defaults.object(forKey: "country") as! String
if (name != "" ){
self.reachability = Reachability.init()
if((self.reachability?.connection) != .none)
{
// let reachability = Reachability2.reachabilityForInternetConnection()
//if reachability.isReachable() || reachability.isReachableViaWiFi() || reachability.isReachableViaWWAN(){
//if (network.reachability.whenReachable != nil) {
let image : NSData = UIImageJPEGRepresentation(profileimg.image!, 32)! as NSData
//let picname = "1"
let grpiconname : String = member_id + name!
let formattedstring = grpiconname.replacingOccurrences(of: " ", with: "_")
let parameters = [
"id" : member_id,
"g_name" : name!,
"type" : "ios",
"country" : country,
"fileName" : formattedstring+".jpg"
]
let url = "http://www.kidsedin.com/nriconnect/webpage/index.php/App/creategroup"
Alamofire.request(url, method: .get, parameters: parameters, encoding: URLEncoding.default).responseJSON { response in
if let j = response.result.value {
actInd.stopAnimating()
//swifty json
// print(j)
let result = (j as AnyObject).stringValue
// print (result)
if result != "0" {
// let urlRequest = self.urlRequestWithComponents(urlString: "http://www.kidsedin.com/nriconnect/webpage/index.php/app/pic_upload?grpicon_name="+formattedstring+"&groupid="+result!, parameters: parameters as NSDictionary)
//print(urlRequest)
// let imageToUploadURL = Bundle.main.url(forResource: "bg", withExtension: "jpg")
//let url = "http://www.kidsedin.com/nriconnect/webpage/index.php/app/testpicupload"
// Use Alamofire to upload the image
print(UIImageJPEGRepresentation(self.profileimg.image!, 1))
Alamofire.upload(multipartFormData: { (multipartFormData) in
multipartFormData.append(UIImageJPEGRepresentation(self.profileimg.image!, 0.2)!, withName: "photo_path", fileName: formattedstring+".jpg", mimeType: "image/jpeg")
for (key, value) in parameters {
multipartFormData.append(value.data(using: .utf8)!, withName: key)
}
}, to:"http://www.kidsedin.com/nriconnect/webpage/index.php/app/testpicupload?grpicon_name="+formattedstring+"&groupid="+result!+"&type=ios")
{ (result) in
switch result {
case .success(let upload, , ):
upload.uploadProgress(closure: { (progress) in
// print (progress)
})
upload.responseJSON { response in
print(response.result.value)
print("1")
}
case .failure(let encodingError): break
//print(encodingError.localizedDescription)
}
}
}
else if result == "0"{
let alert = UIAlertController(title: "", message: "Group name already exist", preferredStyle: .alert)
let okAction = UIAlertAction(title: "Ok", style: .default, handler:nil)
alert.addAction(okAction)
self.present(alert, animated: true, completion: nil)
actInd.stopAnimating()
}else
{
let alert = UIAlertController(title: "", message: "Group creation failed. Something went wrong", preferredStyle: .alert)
let okAction = UIAlertAction(title: "Ok", style: .default, handler: nil)
alert.addAction(okAction)
self.present(alert, animated: true, completion: nil)
actInd.stopAnimating()
}
}
}
}
else
{
//print("Internet connection FAILED")
let alert = UIAlertController(title: "No internet connection. Please check it", message: "Make sure your device is connected to the internet.", preferredStyle: .alert)
let okAction = UIAlertAction(title: "Ok", style: .default, handler: nil)
alert.addAction(okAction)
self.present(alert, animated: true, completion: nil)
actInd.stopAnimating()
}
}
else{
if( name == "" ){
let alert = UIAlertController(title: "", message: "Name field is empty", preferredStyle: .alert)
let okAction = UIAlertAction(title: "Ok", style: .default, handler: nil)
alert.addAction(okAction)
self.present(alert, animated: true, completion: nil)
actInd.stopAnimating()
}
}
}
Выше мой обновленный код.Пожалуйста, предложите, что я должен сделать, чтобы устранить эту ошибку.ИЛИ предоставьте мне альтернативное решение для загрузки этого изображения, используя скрипт php.