How can i load photo from camera or phone library to parse, like PFFile?
How to load image from assets i know, my code:
func loadImage() {
let query = PFQuery(className: "_User")
query.findObjectsInBackground { (objects, error) in
let firstObject = objects?.first as PFObject?
let objectFile = firstObject?.object(forKey: "avatar") as! PFFile
objectFile.getDataInBackground(block: { (imageData, error) in
let image = UIImage(data: imageData!)
if image != nil {
self.avatar.image = image
}
})
}
}
This code upload image from assets. But i need upload from camera or library.
Try this. call displayUploadImageDialog
func on button click. it will open dialog and when you select any image from photos than below delegate
method calls.
func displayUploadImageDialog(btnSelected: UIButton) {
let picker = UIImagePickerController()
picker.delegate = self
picker.allowsEditing = true
let alertController = UIAlertController(title: "", message: "Action on Upload", preferredStyle: .actionSheet)
let cancelAction = UIAlertAction(title: NSLocalizedString("Cancel", comment: "Cancel action"), style: .cancel, handler: {(_ action: UIAlertAction) -> Void in
alertController.dismiss(animated: true) {() -> Void in }
})
alertController.addAction(cancelAction)
let takePhotoAction = UIAlertAction(title: NSLocalizedString("Take Photo", comment: "Take Photo action"), style: .default, handler: {(_ action: UIAlertAction) -> Void in
if UI_USER_INTERFACE_IDIOM() == .pad {
OperationQueue.main.addOperation({() -> Void in
picker.sourceType = .camera
self.present(picker, animated: true) {() -> Void in }
})
}
else {
if !UIImagePickerController.isSourceTypeAvailable(.camera) {
let passwordAlert = UIAlertController(title: "Error", message: "Device has no camera", preferredStyle: .alert)
let yesButton = UIAlertAction(title: "OK", style: .default, handler: {(_ action: UIAlertAction) -> Void in
//Handel your yes please button action here
passwordAlert.dismiss(animated: true) {() -> Void in }
})
passwordAlert.addAction(yesButton)
self.present(passwordAlert, animated: true) {() -> Void in }
}
else {
picker.sourceType = .camera
self.present(picker, animated: true) {() -> Void in }
}
}
})
alertController.addAction(takePhotoAction)
let cameraRollAction = UIAlertAction(title: NSLocalizedString("Camera Roll", comment: "Camera Roll action"), style: .default, handler: {(_ action: UIAlertAction) -> Void in
if UI_USER_INTERFACE_IDIOM() == .pad {
OperationQueue.main.addOperation({() -> Void in
picker.sourceType = .photoLibrary
self.present(picker, animated: true) {() -> Void in }
})
}
else {
picker.sourceType = .photoLibrary
self.present(picker, animated: true) {() -> Void in }
}
})
alertController.addAction(cameraRollAction)
alertController.view.tintColor = Colors.NavTitleColor
present(alertController, animated: true) {() -> Void in }
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
var user = PFUser.current()
let image = info[UIImagePickerControllerOriginalImage] as! UIImage
let imageData = UIImageJPEGRepresentation(image, 0.05)
let imageFile = PFFile(name:"image.jpg", data:imageData!)
user!["profilePicture"] = imageFile;
user?.saveInBackground(block: nil)
self.dismiss(animated: true, completion: nil)
}
func imagePickerControllerDidCancel(picker: UIImagePickerController) {
self.dismiss(animated: true, completion: nil)
}
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.