Saturday, November 21, 2026
HomeiOS Developmentios - Why 1st imageview's pic vanishes if i take pic for...

ios – Why 1st imageview’s pic vanishes if i take pic for 2nd imageview in swift

[ad_1]

Display screen comprises two pictures. right here for profilePicImage if i select from library and for rightProfilePicImg if i take pic with digital camera then profilePicImage pic vanishes why? how ever if i take advantage of library for 2 imageviews then it really works superb

i’m choosing pictures for 2 imageviews like beneath: right here profilePicImage and rightProfilePicImg are twoimageviews

extension ProfileViewController : EasyImagePickerDelegate{

func didSelect(picture: UIImage?, video: URL?, fileName: String?) {
    
    if let img = picture {
        if chooser == .profile {
            profilePicImage.picture = img
        }
        else if chooser == .proper {
            rightProfilePicImg.picture = img
        }
    }
}
}

code for EasyImagePickerDelegate:

public protocol EasyImagePickerDelegate: AnyObject {
    func didSelect(picture: UIImage?, video : URL?, fileName : String?)
}

open class EasyImagePicker: NSObject {

personal let pickerController: UIImagePickerController
personal weak var presentationController: UIViewController?
personal weak var delegate: EasyImagePickerDelegate?

public enum mediaType {
    case pictures
    case video
}

public init(presentationController: UIViewController, delegate: EasyImagePickerDelegate) {
    self.pickerController = UIImagePickerController()
    tremendous.init()
    self.presentationController = presentationController
    self.delegate = delegate
    self.pickerController.delegate = self
    self.pickerController.allowsEditing = true
}

public func current(from sourceView: UIView, mediaType : mediaType, onViewController : UIViewController) {
    sourceView.translatesAutoresizingMaskIntoConstraints = false
    let alertController = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet)
    
    if mediaType == .pictures{
        self.pickerController.mediaTypes = ["public.image"]
        let actionCamera = UIAlertAction(title: "Take Photograph", fashion: .default) { (motion) in
            if AVCaptureDevice.authorizationStatus(for: .video) == .notDetermined{
                let _ = self.checkCameraPermission()
                self.presentAlert(sourceView: sourceView, alertController: alertController)
            }else{
                if self.checkCameraPermission(){
                    self.pickerController.sourceType = .digital camera
                    self.presentationController?.current(self.pickerController, animated: true)
                }else{
                    self.presentCameraSettings(with: "We'd like digital camera entry for capturing image.", fromView: onViewController)
                }
            }
        }
        
        if let actionLibrary  = self.checkPhotoLibrary(isVideo: false, sourceView: sourceView, onViewController: onViewController, alertController: alertController){
            alertController.addAction(actionLibrary)
        }
        if UIImagePickerController.isSourceTypeAvailable(.digital camera){ alertController.addAction(actionCamera) }
    }
    let cancel = UIAlertAction(title: "Cancel", fashion: .cancel, handler: nil)
    cancel.setValue(UIColor.purple, forKey: "titleTextColor")
    alertController.addAction(cancel)
    presentAlert(sourceView: sourceView, alertController: alertController)
}

personal func checkPhotoLibrary(isVideo : Bool,sourceView : UIView,onViewController : UIViewController,alertController : UIAlertController)-> UIAlertAction?{
    let actionLibrary = UIAlertAction(title: "(isVideo ? "Video" : "Photograph") Library", fashion: .default) { (motion) in
        if PHPhotoLibrary.authorizationStatus() == .notDetermined{
            let _ = self.checkPhotoLibraryPermission()
            self.presentAlert(sourceView: sourceView, alertController: alertController)
        }else{
            if self.checkPhotoLibraryPermission(){
                self.pickerController.sourceType = .photoLibrary
                self.presentationController?.current(self.pickerController, animated: true)
            }else{
                self.presentCameraSettings(with: "We gallery permission for choose (isVideo ? "video" : "photograph") from you.", fromView: onViewController)
            }
        }
    }
    return actionLibrary
}

personal func pickerController(_ controller: UIImagePickerController, picture: UIImage?, video : URL?, file : String?) {
    controller.dismiss(animated: true, completion: nil)
    self.delegate?.didSelect(picture: picture, video: video, fileName: file)
}
}

extension EasyImagePicker: UIImagePickerControllerDelegate {
public func imagePickerController(_ picker: UIImagePickerController,
                                  didFinishPickingMediaWithInfo data: [UIImagePickerController.InfoKey: Any]) {
    if let mediaURL = data[.mediaURL] as? URL  {
        self.pickerController(picker, picture: nil, video: mediaURL, file: mediaURL.lastPathComponent)
    }else{
        self.pickerController(picker, picture: nil, video: nil, file: nil)
    }
    guard let picture = data[.editedImage] as? UIImage else {
        return self.pickerController(picker, picture: nil, video: nil, file: nil)
    }
    self.pickerController(picker, picture: picture, video: nil, file: nil)
}
}

[ad_2]

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments