2017-05-24 97 views
2

如何防止用戶在UIImagePickerContoroller中兩次選取相同的圖像以避免重複?防止在UIImagePickerController中選取兩張相同的照片

我試着用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) 
} 

感謝,

+0

爲imagePicker添加完成的代碼。 –

+0

'info [UIImagePickerControllerReferenceURL]'應該對每個圖像都是唯一的,所以你可以保留它來檢查它是否已經被選中 – xmhafiz

+0

已經試過了,當我檢查photosURL.contains(url)是否返回false –

回答

0

好像你還沒有追加的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) 
} 
0

你也應該考慮做picker.dismiss第一和與圖像做其他邏輯之後。這樣,您可以防止用戶多次點擊圖像並多次調用委託功能。

func imagePickerController(_ picker: UIImagePickerController, 
            didFinishPickingMediaWithInfo info: [String : Any]) { 
    picker.dismiss(animated: true) { 
     if let pickedImage = (info[UIImagePickerControllerOriginalImage] as? UIImage) { 
      // do stuff with the picked image 
     } 
    } 
} 
相關問題