I am communicating through web view. My question is to send the pictures from the mobile phone to the web view.
I call API here. I don't know how to send it to the web view. I know how to send only the Key,Value
,which consists of a string.
The code I'm taking pictures of phone.
let imagePicker: UIImagePickerController! = UIImagePickerController()
let imagePicker: UIImagePickerController! = UIImagePickerController()
var captureImage: UIImage!
var flagImageSave = false
@IBAction func btnLoadImageFromLibray(_ sender: UIButton) {
if (UIImagePickerController.isSourceTypeAvailable(.photoLibrary)) {
flagImageSave = false
imagePicker.delegate = self
imagePicker.sourceType = .photoLibrary
imagePicker.mediaTypes = [kUTTypeImage as String]
imagePicker.allowsEditing = true
present(imagePicker, animated: true, completion: nil)
}else{
myAlert("photo album inaccessable", message: "application cannot access the photo album")
}
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
let mediaType = info[UIImagePickerControllerMediaType] as! NSString
if mediaType.isEqual(to: kUTTypeImage as NSString as String){
captureImage = info[UIImagePickerControllerOriginalImage] as! UIImage
if flagImageSave {
UIImageWriteToSavedPhotosAlbum(captureImage, self, nil, nil)
}
imgView.image = captureImage
}else if mediaType.isEqual(to: kUTTypeMovie as NSString as String){
if flagImageSave {
videoURL = (info[UIImagePickerControllerMediaURL] as! URL)
UISaveVideoAtPathToSavedPhotosAlbum(videoURL.relativePath, self, nil, nil)
}
}
self.dismiss(animated: true, completion: nil)
}
API code being received by server on Sping Project
@RequestMapping(value="/sendimage", method = RequestMethod.POST)
public @ResponseBody Map<String, Object> pr_image(HttpServletRequest webRequest
, @RequestParam(value="image", required=false) MultipartFile image
) {
Map<String, Object> param = new HashMap<String, Object>();
Map<String, Object> result = new HashMap<String, Object>();
Map<String, Object> validationMap = ValidationUtils.ValidationOfKeys(webRequest);
if (!validationMap.get("res").equals("sucess")) return validationMap;
String num = (webRequest.getParameter("num") != null) ? webRequest.getParameter("num") : "";
String imagePath = "";
if (image != null) {
String Extension = Config.USER_PROFILE_IMAGE_EXT;
String fileName = "_" + Utils.getCurrentTime("yyyyMMddHHmmssSSS");
imagePath = Define.CONTENTS_FILE_PATH_4 + fileName + Extension ;
File saveDir = new File(Define.CONTENTS_SAVE_PATH + Define.CONTENTS_FILE_PATH_4);
if (!saveDir.isFile()) saveDir.mkdirs();
image.transferTo(new File(Define.CONTENTS_SAVE_PATH + imagePath));
String fileName_thumbnail = fileName + "_thumb" + Extension;
File thumbnail = new File(Define.CONTENTS_SAVE_PATH + Define.CONTENTS_FILE_PATH_4 + fileName_thumbnail);
thumbnail.getParentFile().mkdirs();
Thumbnails.of(saveDir + "/" + fileName + Extension).size(Config.USER_PROFILE_IMAGE_WIDTH, Config.USER_PROFILE_IMAGE_HEIGHT).outputFormat("jpg").toFile(thumbnail);
}
...
How can I transfer pictures with my data to spring server?
I have to send not just images, but also numbers in strings. Look at my server code.
Thanks you in advance
use UIImageJPEGRepresentation
to convert UIImage
to NSData
, and upload use
guard let data = UIImageJPEGRepresentation(image, 0.8) else {
return
}
Alamofire.upload(multipartFormData: { (form) in
form.append(data, withName: "image", mimeType: "image/jpg")
}, to: url) { (result) in
}
You can solve this problem by using the Alamofire
module.
'Alamofire', '~> 4.8.2'
in podfileI'm use Alamofire version 4.8.2
Usage
func ImageUpload(_ image: UIImage) {
guard image.jpegData(compressionQuality: 0.9) != nil else {
self.dismiss(animated: true, completion: nil)
return
}
let imagedata = image.jpegData(compressionQuality: 0.9)
let uploadDict = ["num": "123456789"] as [String:String]
let headers: HTTPHeaders = ["key":"val"] // Use this if you need to add api headers
Alamofire.upload(multipartFormData: { MultipartFormData in
MultipartFormData.append(imagedata!, withName: "image" , fileName: "image.jpg" , mimeType: "image/jpg")
for(key,value) in uploadDict{
MultipartFormData.append(value.data(using: String.Encoding.utf8)!, withName: key)}
},to: "\(url)", headers: headers, encodingCompletion: {
EncodingResult in
switch EncodingResult{
case .success(let upload, _, _):
upload.responseJSON { response in
guard let json = response.result.value! as? [String: Any] else {
return
}
print(json)
}
case .failure(let encodingError):
print("ERROR RESPONSE: \(encodingError)")
}
})
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.