ImageView的UIImagePickerControlle

ImageView的UIImagePickerControlle

我正在尝试从照片库中获取图像,但是有一个问题。我有3种不同的ImageViews。在这些Imageview上有3种不同的轻击手势。我需要选择不同的照片并将其显示给用户。

我该怎么办 ?

最佳答案

UINavigationControllerDelegate, UIImagePickerControllerDelegate添加到您的控制器
创建UITapGesture的函数

func createTapGesture(selectorName: String) -> UITapGestureRecognizer {

    return UITapGestureRecognizer(target: self, action: Selector(selectorName))
}

func imageTap(sender: AnyObject) {

    currentImageView = sender.view as! UIImageView
    pickImage()
}

显示ImagePickerView

func pickImage() {

    let imagePicker = UIImagePickerController()
    imagePicker.delegate = self
    imagePicker.sourceType = UIImagePickerControllerSourceType.PhotoLibrary
    imagePicker.allowsEditing = false
    self.presentViewController(imagePicker, animated: true, completion: nil)
}

func imagePickerController(picker: UIImagePickerController, didFinishPickingImage image: UIImage, editingInfo: [String : AnyObject]?) {

    dismissViewControllerAnimated(true, completion: nil)
    currentImageView.image = image
}

将这些代码添加到viewDidLoad

firstImageView.addGestureRecognizer(createTapGesture("imageTap:"))
secondImageView.addGestureRecognizer(createTapGesture("imageTap:"))
thirdImageView.addGestureRecognizer(createTapGesture("imageTap:"))

在IB中,确保已选中图像视图集User Interaction Enabled


希望这可以帮助

关于ios - 带有ImageView的UIImagePickerController,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/34366792/

10-12 05:05