使用 Alamofire 使用 swift 将多张图片上传到服务器,或者以任何方式从第一步开始实现这一点,以了解我们如何将其与图片选择器一起使用
uploading multi images to the server with swift using Alamofire or anyway to achieve that from the first step to know how we use it with image picker
我需要知道如何从头开始使用 Alamofire 或任何其他方式 select 多张图片并使用其他参数将它们上传到服务器
我真正需要了解的是如何以这种方式使用图像选择器
1-> 在 swift 中使用 imgaePicker
2-> 使用您自己的方式上传图像的完整功能非常感谢,因为我不明白如何从图像选择器中获取图像
最后,我使用名为 OpalImagePicker 的 pod 得到了一个简单的答案,而且使用 Alamofire 的方法非常简单:
代码从头看懂
import Alamofire
import ImagePicker
import Photos
import OpalImagePicker
class PostVC: UIViewController,UITextViewDelegate,UINavigationControllerDelegate,UIImagePickerControllerDelegate, ImagePickerDelegate,OpalImagePickerControllerDelegate {
var arryOfImages = [UIImage]()
// you have to put them don't worry about those funcs
func wrapperDidPress(_ imagePicker: ImagePickerController, images: [UIImage]) {
print("picked")
}
func doneButtonDidPress(_ imagePicker: ImagePickerController, images: [UIImage]) {
print("done")
func cancelButtonDidPress(_ imagePicker: ImagePickerController) {
print("cancel")
}
// 在您的按钮操作中设置以获取图像 image/video .. 等以及它们的数量
@IBAction func addPicture_clicked(_ sender: Any) {
let imagePicker = OpalImagePickerController()
imagePicker.imagePickerDelegate = self
imagePicker.maximumSelectionsAllowed = 3
imagePicker.allowedMediaTypes = Set([PHAssetMediaType.image]) //you can select only images set this
present(imagePicker, animated: true, completion: nil)
}
func imagePicker(_ picker: OpalImagePickerController, didFinishPickingImages images: [UIImage]){
print(images)
self.arryOfImages = images
picker.dismiss(animated: true, completion: nill
}
你想要的要求
//终于请求了,谢谢^^
func addPostClicked(){
guard let text = postTextView.text else {
return
}
let token = "UYJ9ohx_M6JvDbJu0"
let profileId = 104
let params : [String: Any] = [
"postText" :text,
"profileId":profileId
]
Alamofire.upload(
multipartFormData: { multipartFormData in
var count = 1
for img in self.arryOfImages {
//here we send our images
let imageData = img.jpegData(compressionQuality: 0.5)
multipartFormData.append(imageData!, withName: "images[]", fileName: "image\(count).jpeg", mimeType: "image/jpeg")
count += 1
}
for (key, value) in params
{
multipartFormData.append("\(value)".data(using: String.Encoding.utf8)!, withName: key)
}
}, to:"https://api.yoogad.com/rest/api/v1/post/create", method:.post, headers: ["x-auth-token" : token],encodingCompletion: { encodingResult in
switch encodingResult {
case .success(let upload, _, _):
upload.responseJSON { response in
debugPrint(response)
self.dismiss(animated: true)
}
case .failure(let encodingError):
print(encodingError)
}
})
}
我需要知道如何从头开始使用 Alamofire 或任何其他方式 select 多张图片并使用其他参数将它们上传到服务器
我真正需要了解的是如何以这种方式使用图像选择器
1-> 在 swift 中使用 imgaePicker 2-> 使用您自己的方式上传图像的完整功能非常感谢,因为我不明白如何从图像选择器中获取图像
最后,我使用名为 OpalImagePicker 的 pod 得到了一个简单的答案,而且使用 Alamofire 的方法非常简单:
代码从头看懂
import Alamofire
import ImagePicker
import Photos
import OpalImagePicker
class PostVC: UIViewController,UITextViewDelegate,UINavigationControllerDelegate,UIImagePickerControllerDelegate, ImagePickerDelegate,OpalImagePickerControllerDelegate {
var arryOfImages = [UIImage]()
// you have to put them don't worry about those funcs
func wrapperDidPress(_ imagePicker: ImagePickerController, images: [UIImage]) {
print("picked")
}
func doneButtonDidPress(_ imagePicker: ImagePickerController, images: [UIImage]) {
print("done")
func cancelButtonDidPress(_ imagePicker: ImagePickerController) {
print("cancel")
}
// 在您的按钮操作中设置以获取图像 image/video .. 等以及它们的数量
@IBAction func addPicture_clicked(_ sender: Any) {
let imagePicker = OpalImagePickerController()
imagePicker.imagePickerDelegate = self
imagePicker.maximumSelectionsAllowed = 3
imagePicker.allowedMediaTypes = Set([PHAssetMediaType.image]) //you can select only images set this
present(imagePicker, animated: true, completion: nil)
}
func imagePicker(_ picker: OpalImagePickerController, didFinishPickingImages images: [UIImage]){
print(images)
self.arryOfImages = images
picker.dismiss(animated: true, completion: nill
}
你想要的要求
//终于请求了,谢谢^^
func addPostClicked(){
guard let text = postTextView.text else {
return
}
let token = "UYJ9ohx_M6JvDbJu0"
let profileId = 104
let params : [String: Any] = [
"postText" :text,
"profileId":profileId
]
Alamofire.upload(
multipartFormData: { multipartFormData in
var count = 1
for img in self.arryOfImages {
//here we send our images
let imageData = img.jpegData(compressionQuality: 0.5)
multipartFormData.append(imageData!, withName: "images[]", fileName: "image\(count).jpeg", mimeType: "image/jpeg")
count += 1
}
for (key, value) in params
{
multipartFormData.append("\(value)".data(using: String.Encoding.utf8)!, withName: key)
}
}, to:"https://api.yoogad.com/rest/api/v1/post/create", method:.post, headers: ["x-auth-token" : token],encodingCompletion: { encodingResult in
switch encodingResult {
case .success(let upload, _, _):
upload.responseJSON { response in
debugPrint(response)
self.dismiss(animated: true)
}
case .failure(let encodingError):
print(encodingError)
}
})
}