Upload UIImage as Base64 String
(Upload UIImage as string)
//Select Pic From Camera or Gallery
@objc func btnProfileImageSelection(geture:UIGestureRecognizer) {
//Create the AlertController and add Its action like button in Actionsheet
let actionSheetControllerIOS8: UIAlertController = UIAlertController(title: "Choose photo...", message: "", preferredStyle: .actionSheet)
let cancelActionButton = UIAlertAction(title: "Cancel", style: .cancel) { _ in
print("Cancel")
}
actionSheetControllerIOS8.addAction(cancelActionButton)
let saveActionButton = UIAlertAction(title: "Camera", style: .default)
{ _ in
print("Camera")
if UIImagePickerController.isSourceTypeAvailable(UIImagePickerController.SourceType.camera){
let imag = UIImagePickerController()
imag.delegate = self
imag.sourceType = UIImagePickerController.SourceType.camera;
imag.allowsEditing = true
self.present(imag, animated: true, completion: nil)
}
}
actionSheetControllerIOS8.addAction(saveActionButton)
let deleteActionButton = UIAlertAction(title: "Gallery", style: .default)
{ _ in
print("Gallery")
if UIImagePickerController.isSourceTypeAvailable(UIImagePickerController.SourceType.photoLibrary){
let imag = UIImagePickerController()
imag.delegate = self
imag.sourceType = UIImagePickerController.SourceType.photoLibrary;
//imag.mediaTypes = [kUTTypeImage];
imag.allowsEditing = true
self.present(imag, animated: true, completion: nil)
}
}
actionSheetControllerIOS8.addAction(deleteActionButton)
self.present(actionSheetControllerIOS8, animated: true, completion: nil)
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
// The info dictionary may contain multiple representations of the image. You want to use the original.
guard let selectedImage = info[.originalImage] as? UIImage else {
fatalError("Expected a dictionary containing an image, but was provided the following: \(info)")
}
// Set photoImageView to display the selected image.
self.imgUser.image = selectedImage
//Compress image quality
let imageData = selectedImage.jpegData(compressionQuality: 0.1)
//Here Image converted into base64 string
let strBase64 = imageData?.base64EncodedString() ?? "" picker.dismiss(animated: true, completion: nil)
}