2017-07-07 60 views
2

我想上傳第三張,也許更多的圖像到圖像視圖上,但我不知道如何分離第三張圖像。前兩個工作正常,我如何添加第三個功能,我主要需要在func圖像選擇器控制器上的幫助,我認爲。如何分離這些圖像選擇器功能?

var selected = 1 


@IBAction func pickImage1(_ sender: Any) { 


    let image = UIImagePickerController() 
    image.delegate = self 
    image.sourceType = UIImagePickerControllerSourceType.photoLibrary 
    image.allowsEditing = false 
    selected = 1 

    self.present(image, animated: true) 
} 


func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) { 
    if let image = info[UIImagePickerControllerOriginalImage] as? UIImage { 
     if selected == 1 { 
      myImageView1.image = image 
     } else { 
      myImageView2.image = image 
      return 
     } 
     //i tried putting myImageView3 here but it just replaced the 2nd 

    } 
    else { 
     //error 
    } 
    self.dismiss(animated: true, completion: nil) 
} 


@IBAction func pickImage2(_ sender: Any) { 

     let image2 = UIImagePickerController() 
    image2.delegate = self 
    image2.sourceType = UIImagePickerControllerSourceType.photoLibrary 
    image2.allowsEditing = false 
    selected = 2 

    self.present(image2, animated: true) 
} 


@IBAction func pickImage3(_ sender: Any) { 
    let image3 = UIImagePickerController() 
    image3.delegate = self 
    image3.sourceType = UIImagePickerControllerSourceType.photoLibrary 
    image3.allowsEditing = false 
    selected = 3 

    self.present(image3, animated: true) 
} 

回答

2

首先,爲每個圖像視圖添加一個唯一標記。避免使用0,因爲默認的標籤是0。所以,你將有圖像視圖與標籤說,1至3組這樣

enter image description here

在您viewDidLoad每個圖像視圖中添加自來水監聽到所有圖片觀點。然後,在圖像自來水處理程序,圖像視圖通過記錄它的標籤號碼竊聽記錄呼籲所有三個圖像視圖之一這種方法通過一個

let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(imageTapped(tapGestureRecognizer:))) 
imageView.isUserInteractionEnabled = true 
imageView.tag = 1 
imageView.addGestureRecognizer(tapGestureRecognizer) 

。由於標籤號是唯一的,我們可以用它來找出哪些圖像視圖最後,在攝像委託後

func imageTapped(tapGestureRecognizer: UITapGestureRecognizer) 
{ 
    let image = UIImagePickerController() 
    image.delegate = self 
    image.sourceType = UIImagePickerControllerSourceType.photoLibrary 
    image.allowsEditing = false 
    let tappedImage = tapGestureRecognizer.view as! UIImageView 
    selected = tappedImage.tag 
    self.present(image, animated: true) 
} 

挖掘,找到你的圖像視圖從它的父視圖。假設你要添加圖像意見self.view,簡單地得到這樣的看法,並設置圖像

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) { 
    if let image = info[UIImagePickerControllerOriginalImage] as? UIImage { 
     if let imageView = self.view.viewWithTag(selected) as? UIImageView { 
      imageView.image = image 
     } 
    } 
    else { 
     //error 
    } 
    self.dismiss(animated: true, completion: nil) 
} 
+0

獲取錯誤。 selected = tappedImage.tag – Riccardo

+0

@Riccardo你得到了什麼錯誤? –

+0

使用未解析的標識符「selected」 – Riccardo

2

你有很多重複的代碼。創建一個用於選擇圖片的功能。只需選擇每個按鈕就可以保存設置好的圖像視圖。

下面是更新後的代碼:

var selectedImageView: UIImageView? = nil 

@IBAction func pickImage1(_ sender: Any) { 
    selectedImageView = myImageView1 

    selectPicture() 
} 

@IBAction func pickImage2(_ sender: Any) { 
    selectedImageView = myImageView2 

    selectPicture() 
} 

@IBAction func pickImage3(_ sender: Any) { 
    selectedImageView = myImageView3 

    selectPicture() 
} 

func selectImage() { 
    let picker = UIImagePickerController() 
    picker.delegate = self 
    picker.sourceType = .photoLibrary 
    picker.allowsEditing = false 

    present(image, animated: true) 
} 

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) { 
    if let image = info[UIImagePickerControllerOriginalImage] as? UIImage { 
     if let imageView = selectedImageView { 
      imageView.image = image 
     } 
    } 

    selectedImageView = nil 

    self.dismiss(animated: true, completion: nil) 
} 

您還可以,如果你給每個按鈕一個獨特的標籤使用一個按鈕動作。比方說,你給的3個按鍵的標記1,2和3的一個更換3種pickImageX方法:

@IBAction func pickImage(_ sender: UIButton) { 
    switch sender.tag { 
    case 1: 
     selectedImageView = myImageView1 
    case 2: 
     selectedImageView = myImageView2 
    case 3: 
     selectedImageView = myImageView3 
    default: 
     break 
    } 

    if selectedImageView != nil { 
     selectPicture() 
    } 
} 

這裏假設你正在使用這個UIButton。否則根據需要調整。