2017-08-16 47 views
0

我有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。

enter image description here

+0

您可以在父視圖上使用一個輕擊手勢實現相同。我已添加ans供您參考。 – luckyShubhra

回答

0

ImagePickerController給你image從照片庫,這是很好的。像這樣: if let image = info[UIImagePickerControllerOriginalImage] as? UIImage {

ImagePickerController不知道什麼image在做什麼。 您應該指定哪個imageView要顯示image

  1. 聲明一個currentTappedImageView

var currentTappedImageView: UIImageView?

  • 節省ImageView的在點擊
  • currentTappedImageView = tapGestureRecognizer.view as? UIImageView

  • display th Ë圖像從ImagePickerController選擇上currentTappedImageView
  • currentTappedImageView.image = selectedImage

    +0

    謝謝。我明白了。當我執行它時,我面臨另一個問題。 我需要將GestureRecognizer添加到所有UIImageView。當我喜歡下面的時候,我只能點擊最後一個UIImageView,它是plus1UIImageView。 你知道爲什麼嗎?由於 -------- cameraUIImageView.isUserInteractionEnabled =真 cameraUIImageView.addGestureRecognizer(tapGestureRecognizer) plus1UIImageView.isUserInteractionEnabled =真 plus1UIImageView.addGestureRecognizer(tapGestureRecognizer) – John

    +0

    你需要多'tapGestureRecognizer'。但是所有'tapGestureRecognizer'都可以使用相同的回調(選擇器)。 – Codus

    0

    你只需要一個imagePickerController,如果使用的CollectionView,您應該保存indexPath用戶點擊,當使用3的UIImageView,你應該用戶選擇保存後的UIImageView的ID和設置圖像到它。

    希望這有助於你

    0

    你只需要UIImagePickerviewController的一個實例。但是你必須跟蹤你填寫的圖片視圖。

    1

    您可以將所有UIImageView添加爲主視圖的子視圖。將UITapGestureRecognizer添加到主視圖。當用戶點擊主視圖時,複製點擊的圖像視圖的ID並使用它來顯示圖像。

    var activeImageView:UIImageView? 
    
    let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(cameraTapped(tapGestureRecognizer:)))   
    mainView.isUserInteractionEnabled = true // mainView is super view of all 3 imageViews. 
    mainView.addGestureRecognizer(tapGestureRecognizer) 
    

    你可以從則hitTest您挖掘的ImageView:方法

    func cameraTapped(tapGestureRecognizer: UITapGestureRecognizer) 
    { 
        let view = tapGestureRecognizer.view 
        let loc = tapGestureRecognizer.location(in: view) 
        activeImageView = view?.hitTest(loc, with: nil) as? UIImageView 
        //.... your code 
    } 
    

    ,並設置圖像的活動圖像視圖

    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) { 
         if let image = info[UIImagePickerControllerOriginalImage] as? UIImage { 
          selectedImage = image 
          activeImageView.image = image 
          //.... your code 
         } 
    } 
    

    做讓我知道,如果您有任何疑問在評論中,我會盡力幫助。

    相關問題