如何用用户选择的照片替换图像
Posted
技术标签:
【中文标题】如何用用户选择的照片替换图像【英文标题】:how to replace images with user selected photos 【发布时间】:2022-01-14 21:23:27 【问题描述】: var images : [UIImage?] = [nil, nil, nil, nil, nil]
我有图像数组。我想通过按钮 sender.tag 将 nil 替换为图像
@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)
如果 array[index] 为 nil,则显示 + 图像。 但是array[index]是一张照片,显示照片
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
//
这段代码是 PHPicerViewControllerDelegate (https://ikyle.me/blog/2020/phpickerviewcontroller)
【问题讨论】:
当您点击添加按钮并将图像放置在所选索引上时使用所选索引 【参考方案1】:您可以通过以下方法做到这一点 为您的 UIButtons 分配标签,并在 UIButton 操作上通过管理对象将您的图像添加到数组的特定索引上
例如
var selectedIndex or selectedTag // of your UIButton
点击按钮更新您的 selectedIndex 值
self.selectedIndex = sender.tag
然后在您的 didFinishPicking 方法中使用 selectedIndex 上的选定图像更新您的图像数组,并使用以下方式更新您的按钮图像
images.insert(yourSelectedImage, at: selectedIndex)
self.setImagesForButtons()
现在使用 UIButton Outlet 将您选择的图像分配给 UIButton 例如,您的五个按钮有五个插座
@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
【讨论】:
【参考方案2】:您可以在构建图像选择器中使用
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])
)
【讨论】:
如果您需要自定义警报功能,请告诉我【参考方案3】:给出了正确的答案,只是想让它更短。
每个按钮都应该有一个标签,从 0 到 4。
然后你可以在你的控制器中创建一个属性。让它成为selectedIndex
。在 addImage 中,您应该添加以下行:
@IBAction func addImage(_ sender: UIButton)
self.selectedIndex = _sender.tag
....
然后在你得到图像后的委托方法中:
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)
【讨论】:
以上是关于如何用用户选择的照片替换图像的主要内容,如果未能解决你的问题,请参考以下文章