防止在 UIImagePickerController 中选择同一张照片两次
prevent picking the same photo twice in UIImagePickerController
如何防止用户在 UIImagePickerContoroller 中选择相同的图像两次以避免重复?
我试着用 URLReference 来做,但它不起作用,所以我猜它不是这样。
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
if let url = info[UIImagePickerControllerReferenceURL] as? NSURL{
if photosURL.contains(url){
Utilities.showMessage(message: "photo Uploaded already", sender: self, title: ErrorTitle.FRIENDS, onDismissAction: nil)
} else {
if let pickedImage = info[UIImagePickerControllerOriginalImage] as? UIImage {
photos.append(pickedImage)
}
}
}
dismiss(animated: true, completion: nil)
}
谢谢,
您似乎没有将 url 附加到 photosURL?
试试这个:
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
if let url = info[UIImagePickerControllerReferenceURL] as? NSURL{
if photosURL.contains(url){
Utilities.showMessage(message: "photo Uploaded already", sender: self, title: ErrorTitle.FRIENDS, onDismissAction: nil)
} else {
photosURL.append(url)
if let pickedImage = info[UIImagePickerControllerOriginalImage] as? UIImage {
photos.append(pickedImage)
}
}
}
dismiss(animated: true, completion: nil)
}
您还应该考虑先执行 picker.dismiss
,然后再对图像执行其他逻辑。这样,您就可以防止用户多次点击图像并多次调用委托函数。
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
guard !picker.isBeingDismissed else {
return
}
picker.dismiss(animated: true) {
if let pickedImage = (info[UIImagePickerController.InfoKey(rawValue: UIImagePickerController.InfoKey.originalImage.rawValue)] as? UIImage) {
// do stuff with the picked image
print("Uesr picked an image \(pickedImage)")
}
}
}
swift 4
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
let info = convertFromUIImagePickerControllerInfoKeyDictionary(info)
if let capturedImage = info[convertFromUIImagePickerControllerInfoKey(UIImagePickerController.InfoKey.originalImage)] as? UIImage {
// do stuff
}
picker.delegate = nil
picker.dismiss(animated: true, completion: nil)
}
如何防止用户在 UIImagePickerContoroller 中选择相同的图像两次以避免重复?
我试着用 URLReference 来做,但它不起作用,所以我猜它不是这样。
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
if let url = info[UIImagePickerControllerReferenceURL] as? NSURL{
if photosURL.contains(url){
Utilities.showMessage(message: "photo Uploaded already", sender: self, title: ErrorTitle.FRIENDS, onDismissAction: nil)
} else {
if let pickedImage = info[UIImagePickerControllerOriginalImage] as? UIImage {
photos.append(pickedImage)
}
}
}
dismiss(animated: true, completion: nil)
}
谢谢,
您似乎没有将 url 附加到 photosURL? 试试这个:
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
if let url = info[UIImagePickerControllerReferenceURL] as? NSURL{
if photosURL.contains(url){
Utilities.showMessage(message: "photo Uploaded already", sender: self, title: ErrorTitle.FRIENDS, onDismissAction: nil)
} else {
photosURL.append(url)
if let pickedImage = info[UIImagePickerControllerOriginalImage] as? UIImage {
photos.append(pickedImage)
}
}
}
dismiss(animated: true, completion: nil)
}
您还应该考虑先执行 picker.dismiss
,然后再对图像执行其他逻辑。这样,您就可以防止用户多次点击图像并多次调用委托函数。
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
guard !picker.isBeingDismissed else {
return
}
picker.dismiss(animated: true) {
if let pickedImage = (info[UIImagePickerController.InfoKey(rawValue: UIImagePickerController.InfoKey.originalImage.rawValue)] as? UIImage) {
// do stuff with the picked image
print("Uesr picked an image \(pickedImage)")
}
}
}
swift 4
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
let info = convertFromUIImagePickerControllerInfoKeyDictionary(info)
if let capturedImage = info[convertFromUIImagePickerControllerInfoKey(UIImagePickerController.InfoKey.originalImage)] as? UIImage {
// do stuff
}
picker.delegate = nil
picker.dismiss(animated: true, completion: nil)
}