Reputation:
I have a view controller with UIImagePickerControllerDelegate and UINavigationControllerDelegate to pick photo.
But when I pick photo and then it auto save a same photo in my photo album.
I guess I write this function in my code
UIImageWriteToSavedPhotosAlbum(photoImage!, nil, nil, nil)
so, I check sourcetype equal to camera then save the photo and pick photo in album not save the same photo again .
But it doesn't work. what's happen?
It also auto save the same photo.
I don't want to save the same photo?
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
let uuid = NSUUID().uuidString
let imageName:String = uuid + ".jpg"
let documentsPath = NSHomeDirectory().appending("/Documents/\(image)/")
let imagePath = documentsPath.appending(imageName)
let imageUrl = URL(fileURLWithPath: imagePath)
print("imageUrl is here:\(imageUrl)")
photoImage = info[UIImagePickerControllerOriginalImage] as? UIImage
if (UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.camera)) {
photoImage = info[UIImagePickerControllerOriginalImage] as? UIImage
// this doesn't work!!!!
//UIImageWriteToSavedPhotosAlbum(photoImage!, nil, nil, nil)
}
//save image to local Documents
let imageData:Data = UIImageJPEGRepresentation(photoImage!, 0.05)!
do {
try imageData.write(to: imageUrl,options: .atomic)
} catch let error {
print(error)
}
showLoading()
hideLoading {
//I have func upload image to server
self.tableView.reloadData()
self.dismiss(animated: true, completion: nil)
}
}
func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
self.dismiss(animated: true, completion: nil)
}
Upvotes: 0
Views: 85
Reputation: 318774
You are not checking the image picker controller's source type, you are checking if the camera is available in general.
Change this:
if (UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.camera)) {
to:
if picker.sourceType == .camera {
Upvotes: 1