我有1周的UIImageView,用戶可以點擊的UIImageView選擇從照片庫照片,抽頭3不同的UIImageView選擇並顯示照片
let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(cameraTapped(tapGestureRecognizer:)))
cameraUIView.isUserInteractionEnabled = true
cameraUIView.addGestureRecognizer(tapGestureRecognizer)
其中cameraTapped是
func cameraTapped(tapGestureRecognizer: UITapGestureRecognizer)
{
let imagePicker = UIImagePickerController()
imagePicker.delegate = self
let actionSheet = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet)
actionSheet.addAction(UIAlertAction(title: "寫真を撮る", style: .default, handler: { (action:UIAlertAction) in
if UIImagePickerController.isSourceTypeAvailable(.camera) {
imagePicker.sourceType = .camera
imagePicker.allowsEditing = true
self.present(imagePicker,animated: true,completion: nil)
}
}))
actionSheet.addAction(UIAlertAction(title: "アルバムから選択する", style: .default, handler: { (action:UIAlertAction) in
if UIImagePickerController.isSourceTypeAvailable(.photoLibrary) {
imagePicker.sourceType = .photoLibrary
self.present(imagePicker,animated: true,completion: nil)
}
}))
actionSheet.addAction(UIAlertAction(title: "キャンセル", style: .cancel, handler: nil))
self.present(actionSheet, animated: true, completion: nil)
和
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
if let image = info[UIImagePickerControllerOriginalImage] as? UIImage {
selectedImage = image
cameraUIImageView.image = image
cameraUIImageView.contentMode = .scaleAspectFill
cameraUIImageView.clipsToBounds = true
let leadingConstrain = NSLayoutConstraint(item: cameraUIImageView, attribute: .leading, relatedBy: .equal, toItem: cameraUIImageView.superview, attribute: .leading, multiplier: 1, constant: 0)
leadingConstrain.isActive = true
let trailingConstrain = NSLayoutConstraint(item: cameraUIImageView, attribute: .trailing, relatedBy: .equal, toItem: cameraUIImageView.superview, attribute: .trailing, multiplier: 1, constant: 0)
trailingConstrain.isActive = true
let topConstrain = NSLayoutConstraint(item: cameraUIImageView, attribute: .top, relatedBy: .equal, toItem: cameraUIImageView.superview, attribute: .top, multiplier: 1, constant: 0)
topConstrain.isActive = true
let bottomConstrain = NSLayoutConstraint(item: cameraUIImageView, attribute: .bottom, relatedBy: .equal, toItem: cameraUIImageView.superview, attribute: .bottom, multiplier: 1, constant: 0)
bottomConstrain.isActive = true
}
dismiss(animated: true, completion: nil)
cameraLabel.text = ""
}
This Works。但是,我想擁有3個UIImageView,點擊每個UIImageView將讓用戶選擇照片並在UIImageVIew上顯示該照片。
我是否需要創建3個不同的imagePickerController? 那該怎麼做?,因爲imagePickerController執行的是UIImagePickerDelegate。
您可以在父視圖上使用一個輕擊手勢實現相同。我已添加ans供您參考。 – luckyShubhra