Screen contains two images. here for profilePicImage
if i choose from library and for rightProfilePicImg
if i take pic with camera then profilePicImage
pic vanishes why? how ever if i use library for two imageviews then it works fine
i am picking images for two imageviews like below: here profilePicImage
and rightProfilePicImg
are twoimageviews
extension ProfileViewController : EasyImagePickerDelegate{
func didSelect(image: UIImage?, video: URL?, fileName: String?) {
if let img = image {
if chooser == .profile {
profilePicImage.image = img
}
else if chooser == .right {
rightProfilePicImg.image = img
}
}
}
}
code for EasyImagePickerDelegate:
public protocol EasyImagePickerDelegate: AnyObject {
func didSelect(image: UIImage?, video : URL?, fileName : String?)
}
open class EasyImagePicker: NSObject {
private let pickerController: UIImagePickerController
private weak var presentationController: UIViewController?
private weak var delegate: EasyImagePickerDelegate?
public enum mediaType {
case images
case video
}
public init(presentationController: UIViewController, delegate: EasyImagePickerDelegate) {
self.pickerController = UIImagePickerController()
super.init()
self.presentationController = presentationController
self.delegate = delegate
self.pickerController.delegate = self
self.pickerController.allowsEditing = true
}
public func present(from sourceView: UIView, mediaType : mediaType, onViewController : UIViewController) {
sourceView.translatesAutoresizingMaskIntoConstraints = false
let alertController = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet)
if mediaType == .images{
self.pickerController.mediaTypes = ["public.image"]
let actionCamera = UIAlertAction(title: "Take Photo", style: .default) { (action) in
if AVCaptureDevice.authorizationStatus(for: .video) == .notDetermined{
let _ = self.checkCameraPermission()
self.presentAlert(sourceView: sourceView, alertController: alertController)
}else{
if self.checkCameraPermission(){
self.pickerController.sourceType = .camera
self.presentationController?.present(self.pickerController, animated: true)
}else{
self.presentCameraSettings(with: "We need camera access for capturing picture.", fromView: onViewController)
}
}
}
if let actionLibrary = self.checkPhotoLibrary(isVideo: false, sourceView: sourceView, onViewController: onViewController, alertController: alertController){
alertController.addAction(actionLibrary)
}
if UIImagePickerController.isSourceTypeAvailable(.camera){ alertController.addAction(actionCamera) }
}
let cancel = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)
cancel.setValue(UIColor.red, forKey: "titleTextColor")
alertController.addAction(cancel)
presentAlert(sourceView: sourceView, alertController: alertController)
}
private func checkPhotoLibrary(isVideo : Bool,sourceView : UIView,onViewController : UIViewController,alertController : UIAlertController)-> UIAlertAction?{
let actionLibrary = UIAlertAction(title: "\(isVideo ? "Video" : "Photo") Library", style: .default) { (action) in
if PHPhotoLibrary.authorizationStatus() == .notDetermined{
let _ = self.checkPhotoLibraryPermission()
self.presentAlert(sourceView: sourceView, alertController: alertController)
}else{
if self.checkPhotoLibraryPermission(){
self.pickerController.sourceType = .photoLibrary
self.presentationController?.present(self.pickerController, animated: true)
}else{
self.presentCameraSettings(with: "We gallery permission for pick \(isVideo ? "video" : "photo") from you.", fromView: onViewController)
}
}
}
return actionLibrary
}
private func pickerController(_ controller: UIImagePickerController, image: UIImage?, video : URL?, file : String?) {
controller.dismiss(animated: true, completion: nil)
self.delegate?.didSelect(image: image, video: video, fileName: file)
}
}
extension EasyImagePicker: UIImagePickerControllerDelegate {
public func imagePickerController(_ picker: UIImagePickerController,
didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey: Any]) {
if let mediaURL = info[.mediaURL] as? URL {
self.pickerController(picker, image: nil, video: mediaURL, file: mediaURL.lastPathComponent)
}else{
self.pickerController(picker, image: nil, video: nil, file: nil)
}
guard let image = info[.editedImage] as? UIImage else {
return self.pickerController(picker, image: nil, video: nil, file: nil)
}
self.pickerController(picker, image: image, video: nil, file: nil)
}
}
CodePudding user response:
Try this:
extension EasyImagePicker: UIImagePickerControllerDelegate {
public func imagePickerController(_ picker: UIImagePickerController,
didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey: Any]) {
if let mediaURL = info[.mediaURL] as? URL {
self.pickerController(picker, image: nil, video: mediaURL, file: mediaURL.lastPathComponent)
}else{
guard let image = info[.editedImage] as? UIImage else {
return self.pickerController(picker, image: nil, video: nil, file: nil)
}
self.pickerController(picker, image: image, video: nil, file: nil)
}
}
}