Home > Software design >  how to replace images with user selected photos
how to replace images with user selected photos

Time:12-10

 var images      : [UIImage?] = [nil, nil, nil, nil, nil]

 

I have image array. I want to replace nil to image by button sender.tag

@IBAction func addImage(_ sender: UIButton) {
        var configuration = PHPickerConfiguration()
        configuration.selectionLimit = 1
        configuration.filter = .any(of: [.images])
        
        let picker = PHPickerViewController(configuration: configuration)
        picker.delegate = self
        self.present(picker, animated: true, completion: nil)

Add image buttons

If array[index] is nil, show image. But array[index] is a photo, show photo

extension EditViewController: PHPickerViewControllerDelegate {
    @available(iOS 14, *)
    func picker(_ picker: PHPickerViewController, didFinishPicking results: [PHPickerResult]) {
        picker.dismiss(animated: true, completion: nil)
        
        let itemProvider = results.first?.itemProvider
        
        if let itemProvider = itemProvider,
           itemProvider.canLoadObject(ofClass: UIImage.self) {
            itemProvider.loadObject(ofClass: UIImage.self) { (object, error) in
                if let image = object as? UIImage {
                    DispatchQueue.main.async {
                        print("image: \(image)")
                        
                        
                        print("IMAGES ARRAY : \(self.images)")
                        
                    }
                }
                
            }
        } else {
            //
        }
        
    }
}

This code is PHPicerViewControllerDelegate (https://ikyle.me/blog/2020/phpickerviewcontroller)

CodePudding user response:

you can do this with following method assign tags to your UIButtons and on UIButton action add your image on specific index of array by managing an object

e.g

var selectedIndex or selectedTag // of your UIButton

on Button click update your selectedIndex value with

self.selectedIndex = sender.tag

then in your didFinishPicking method update your images array with selected image on selectedIndex and update your buttons images with following way

images.insert(yourSelectedImage, at: selectedIndex)
self.setImagesForButtons()

now assign your selected image to UIButton using UIButton Outlet e.g you have five outlets for your five buttons

@IBOutlet weak var yourButtonOutlet1: UIButton!
@IBOutlet weak var yourButtonOutlet2: UIButton!
@IBOutlet weak var yourButtonOutlet3: UIButton!
@IBOutlet weak var yourButtonOutlet4: UIButton!
@IBOutlet weak var yourButtonOutlet5: UIButton!



 func setImagesForButtons()
    {
        if (self.selectedIndex == 0)
        {
            if (images[selectedIndex] != nil)
            {
                
                self.yourButtonOutlet1.setImage(images[selectedIndex], for: .normal)
            }
            else
            {
                // set plus icon image to your button
            }
        }
        else if (self.selectedIndex == 1)
        {
            if (images[selectedIndex] != nil)
            {
                
                self.yourButtonOutlet2.setImage(images[selectedIndex], for: .normal)
            }
            else
            {
                // set plus icon image to your button
            }
        }
        else if (self.selectedIndex == 2)
        {
            if (images[selectedIndex] != nil)
            {
                
                self.yourButtonOutlet3.setImage(images[selectedIndex], for: .normal)
            }
            else
            {
                // set plus icon image to your button
            }
        }
        else if (self.selectedIndex == 3)
        {
            if (images[selectedIndex] != nil)
            {
                
                self.yourButtonOutlet4.setImage(images[selectedIndex], for: .normal)
            }
            else
            {
                // set plus icon image to your button
            }
        }
        else if (self.selectedIndex == 4)
        {
            if (images[selectedIndex] != nil)
            {
                
                self.yourButtonOutlet5.setImage(images[selectedIndex], for: .normal)
            }
            else
            {
                // set plus icon image to your button
            }
        }
    }

CodePudding user response:

You can use in build Image Picker

extension EditProfileViewController: UIImagePickerControllerDelegate,UINavigationControllerDelegate {
    
    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
        
//        localUserData.ProfileImage = info[.originalImage] as? UIImage
        self.imgProfile.image = info[.originalImage] as? UIImage
        self.dismiss(animated: true, completion: {
            let yes = UIAlertAction(title: "Yes", style: .default, handler: { _ in
                self.imgProfile.image = self.localUserData.ProfileImage
            })
            let no = UIAlertAction(title: "No", style: .default, handler: nil)
            self.showCustomAlert(alertMsg: ValidationMsg.imageUpload.rawValue, actions: [yes,no])
        })
    }
}

CodePudding user response:

The correct answer was given, just want to make it shorter.

The every button should have a tag, from 0 to 4.

Then you can create a property in your controller. Let it be selectedIndex. In the addImage you should add the line:

@IBAction func addImage(_ sender: UIButton) {
    self.selectedIndex = _sender.tag
    ....
}

Then in the delegate method after you got an image:

images[selectedIndex] = image
let buttons = [button1, button2, button3, button4, button5]
let selectedButton = buttons[selectedIndex]

if (images[selectedIndex] != nil) {   
    selectedButton.setImage(images[selectedIndex], for: .normal)
}
else {
    selectedButton.setImage(imagePlus, for: .normal)
}
  • Related