Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Swift Sending Photos to Webservice (Photo to StringBase64) -- http://www.bulentsiyah.com/swift-sending-photos-to-webservice-photo-to-stringbase64/
@IBOutlet weak var imageSelected: UIImageView!
var imagePicker = UIImagePickerController()
var baseImage: UIImage!
// didload
imagePicker.delegate = self
NotificationCenter.default.addObserver(self, selector: #selector(BildirTableViewController.resimGoster), name: NSNotification.Name(rawValue: "resimGoster"), object: nil)
//
@objc func resimGoster() {
do{
//self.boxView.removeFromSuperview()
self.imageSelected.image = self.baseImage
}catch{}
}
//
let alert:UIAlertController=UIAlertController(title: "Seçiniz", message: nil, preferredStyle: UIAlertControllerStyle.actionSheet)
let cameraAction = UIAlertAction(title: "Kamera", style: UIAlertActionStyle.default)
{
UIAlertAction in
self.openCamera(self.imagePicker)
}
let galeriAction = UIAlertAction(title: "Galeri", style: UIAlertActionStyle.default)
{
UIAlertAction in
self.openGalery(self.imagePicker)
}
let cancelAction = UIAlertAction(title: "İptal", style: UIAlertActionStyle.cancel)
{
UIAlertAction in
}
// Add the actions
alert.addAction(cameraAction)
alert.addAction(galeriAction)
alert.addAction(cancelAction)
self.present(alert, animated: true, completion: nil)
//
func openCamera(_ imagePicker: UIImagePickerController)
{
if(UIImagePickerController .isSourceTypeAvailable(UIImagePickerControllerSourceType.camera))
{
imagePicker.sourceType = UIImagePickerControllerSourceType.camera
imagePicker.allowsEditing = true
self .present(imagePicker, animated: true, completion: nil)
}
}
func openGalery(_ imagePicker: UIImagePickerController)
{
if(UIImagePickerController .isSourceTypeAvailable(UIImagePickerControllerSourceType.photoLibrary))
{
imagePicker.sourceType = UIImagePickerControllerSourceType.photoLibrary
imagePicker.allowsEditing = true
self .present(imagePicker, animated: true, completion: nil)
}
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
//imageView.image = info[UIImagePickerControllerEditedImage] as? UIImage
baseImage = info[UIImagePickerControllerEditedImage] as? UIImage
StaticObject.gonderilcekNotification.PhotoGuid = CFUUIDCreateString(nil, CFUUIDCreate(nil)) as String
// addSavingPhotoView()
self.asyncInitializeUpload(self.baseImage){
(result:String) in
if(result == "success"){
NotificationCenter.default.post(name: NSNotification.Name(rawValue:"resimGoster"), object: nil)
}
}
self.dismiss(animated: true, completion: nil)
}
func addSavingPhotoView() {
// You only need to adjust this frame to move it anywhere you want
boxView = UIView(frame: CGRect(x: view.frame.midX - 90, y: view.frame.midY - 95, width: 180, height: 50))
boxView.backgroundColor = UIColor.white
boxView.alpha = 0.8
boxView.layer.cornerRadius = 10
//Here the spinnier is initialized
let activityView = UIActivityIndicatorView(activityIndicatorStyle: UIActivityIndicatorViewStyle.gray)
activityView.frame = CGRect(x: 0, y: 0, width: 50, height: 50)
activityView.startAnimating()
let textLabel = UILabel(frame: CGRect(x: 45, y: 0, width: 200, height: 50))
textLabel.textColor = UIColor.gray
textLabel.text = "Lütfen Bekleyin"
boxView.addSubview(activityView)
boxView.addSubview(textLabel)
view.addSubview(boxView)
}
func asyncInitializeUpload(_ baseImage: UIImage, completion: @escaping (_ : String) -> Void) {
DispatchQueue.global(qos: .background).async {
var imageData: Data = UIImageJPEGRepresentation(baseImage, 0.1)!
//var dataToService2
StaticObject.gonderilcekNotification.Photo = imageData.base64EncodedString(options: Data.Base64EncodingOptions.lineLength64Characters)
completion("success")
/* self.uploadPhoto(dataToService2, fotografliBilgiUID: self.fotografliBilgiUID, isFinal: true){
(result:String) in
if(result == "success"){
// completion("success")
}else{
self.boxView.removeFromSuperview()
self.mesajGoster(result)
// completion("fail")
}
}*/
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.