我想将背景图片添加到UIImageView
。目前,在UIImageView
的空框架中,我有tapGestureRcognizer
打开相机。
let imageView = whiskyPicture
let tapGestureRecognizer = UITapGestureRecognizer(target:self, action:#selector(DataInsertController.imageTapped(_:)))
imageView!.userInteractionEnabled = true
imageView!.addGestureRecognizer(tapGestureRecognizer)
func imageTapped(sender: UITapGestureRecognizer) {
imagePicker = UIImagePickerController()
imagePicker.allowsEditing = true
imagePicker.delegate = self
imagePicker.sourceType = .Camera
presentViewController(imagePicker, animated: true, completion: nil)
现在我想在UIImageView
中添加一个背景图片,用户可以看到在哪里点按。最好的办法是将iOS的相机图标添加到UIImage中。选择照片后,背景图像应该消失。
有任何建议如何做到这一点?
故事板看起来像这样:
在UIImageView
我想添加背景图片。
答案 0 :(得分:1)
在您的imageView
中直接设置相机图片并将Mode
属性设置为Center
。就像这个图像一样
现在在didFinishPickingMediaWithInfo
方法中,只需将contentMode
的{{1}}更改为您想要的内容imageView
,Scale To Fill
,Aspect Fit
等
答案 1 :(得分:0)
首先,设计或搜索相机图像并将其添加到图像资源中。假设您将文件命名为camera.jpg,在故事板中将图像设置为camera.jpg。因此,在加载时,您将看到该图像。
现在,当你完成拍照时,你想用你的“拍摄”照片替换camera.jpg。
func imagePickerController(picker: UIImagePickerController!, didFinishPickingMediaWithInfo info:NSDictionary!) {
let tempImage = editingInfo[UIImagePickerControllerOriginalImage] as UIImage
myImageView.image = tempImage
self.dismissModalViewControllerAnimated(true)
}
答案 2 :(得分:0)
首先,您需要在类中实现UIImagePickerControllerDelegate。
var imagePicker : UIImagePickerController!
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
imagePicker.dismissViewControllerAnimated(true, completion: nil)
imageView.image = info[UIImagePickerControllerOriginalImage] as? UIImage
}
override func viewWillAppear(animated: Bool) {
//Makes the camera pops up
if imagePicker == nil {
imagePicker = UIImagePickerController()
imagePicker.delegate = self
imagePicker.sourceType = .Camera
presentViewController(imagePicker, animated: true, completion: nil) }
}
现在使用可以拍照,图片将显示在imageView中。希望我能正确理解这个问题