我正在用Xcode快速编写代码,并尝试创建一个具有两个UIImageViews的“编辑用户个人资料ViewController”,一个UIImageView用于个人资料照片,另一个用于封面照片。我很难更改封面照片的图像。当我进入模拟器并单击“更改封面照片”按钮并选择图像后,它将更改个人资料照片。当我单击更改个人资料照片按钮并选择图像时,它也会同时更改个人资料图像,但此操作是正确的。有谁知道如何帮助我在哪里可以更改这些控制器的图像,而不是让它们成为同一图像?这是我的下面的代码。
import UIKit
class profilephotoViewController: UIViewController {
@IBOutlet weak var profileImagePhoto: UIImageView!
@IBOutlet weak var profileImageTapped: UIButton!
var imagePicker:UIImagePickerController!
@IBOutlet weak var coverImagePhoto: UIImageView!
@IBOutlet weak var coverImageTapped: UIButton!
var secondImagePicker:UIImagePickerController!
override func viewDidLoad() {
super.viewDidLoad()
let imageTap = UITapGestureRecognizer(target: self, action: #selector(openImagePicker))
profileImagePhoto.isUserInteractionEnabled = true
profileImagePhoto.addGestureRecognizer(imageTap)
profileImagePhoto.layer.cornerRadius = profileImagePhoto.bounds.height / 2
profileImagePhoto.clipsToBounds = true
profileImageTapped.addTarget(self, action: #selector(openImagePicker), for: .touchUpInside)
imagePicker = UIImagePickerController()
imagePicker.allowsEditing = true
imagePicker.sourceType = .photoLibrary
imagePicker.delegate = self
let coverTap = UITapGestureRecognizer(target: self, action: #selector(openSecondImagePicker))
coverImagePhoto.isUserInteractionEnabled = true
coverImagePhoto.addGestureRecognizer(coverTap)
coverImagePhoto.clipsToBounds = true
coverImageTapped.addTarget(self, action: #selector(openSecondImagePicker), for: .touchUpInside)
secondImagePicker = UIImagePickerController()
secondImagePicker.allowsEditing = true
secondImagePicker.sourceType = .photoLibrary
secondImagePicker.delegate = self
// Do any additional setup after loading the view.
}
@objc func openImagePicker(_ sender:Any) {
//Open Image Picker
self.present(imagePicker, animated: true, completion: nil)
}
@objc func openSecondImagePicker(_ sender:Any) {
//Open Second Image Picker
self.present(secondImagePicker, animated: true, completion: nil)
}
}
extension profilephotoViewController: UIImagePickerControllerDelegate, UINavigationControllerDelegate {
func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
picker.dismiss(animated: true, completion: nil)
}
internal func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
if let pickedImage = info[UIImagePickerController.InfoKey.editedImage] as? UIImage {
self.profileImagePhoto.image = pickedImage
}
picker.dismiss(animated: true, completion: nil)
}
func secondImagePickerControllerDidCancel(_ picker: UIImagePickerController) {
picker.dismiss(animated: true, completion: nil)
}
internal func secondImagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
if let secondPickedImage = info[UIImagePickerController.InfoKey.editedImage] as? UIImage {
self.coverImagePhoto.image = secondPickedImage
}
picker.dismiss(animated: true, completion: nil)
}
}
答案 0 :(得分:0)
您可以尝试
internal func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
if let secondPickedImage = info[UIImagePickerController.InfoKey.editedImage] as? UIImage {
if let picker == imagePicker {
self.profileImagePhoto.image = secondPickedImage
}
else {
self.coverImagePhoto.image = secondPickedImage
}
}
picker.dismiss(animated: true, completion: nil)
}