提交 1f78e59c 编写于 作者: J John Hampton

Adds an Image using tap gesture

上级 f22720d9
......@@ -40,11 +40,15 @@
<action selector="setDefaultLabelText:" destination="BYZ-38-t0r" eventType="touchUpInside" id="fTO-ku-xZY"/>
</connections>
</button>
<imageView userInteractionEnabled="NO" contentMode="scaleToFill" horizontalHuggingPriority="251" verticalHuggingPriority="251" placeholderIntrinsicWidth="320" placeholderIntrinsicHeight="320" image="defaultPhoto" translatesAutoresizingMaskIntoConstraints="NO" id="Rhj-yY-68e">
<imageView contentMode="scaleToFill" horizontalHuggingPriority="251" verticalHuggingPriority="251" placeholderIntrinsicWidth="320" placeholderIntrinsicHeight="320" image="defaultPhoto" translatesAutoresizingMaskIntoConstraints="NO" id="Rhj-yY-68e">
<rect key="frame" x="0.0" y="104.5" width="320" height="320"/>
<gestureRecognizers/>
<constraints>
<constraint firstAttribute="width" secondItem="Rhj-yY-68e" secondAttribute="height" multiplier="1:1" id="ACo-ke-FCa"/>
</constraints>
<connections>
<outletCollection property="gestureRecognizers" destination="UAM-fK-D64" appends="YES" id="uEG-WL-m3E"/>
</connections>
</imageView>
</subviews>
<constraints>
......@@ -63,9 +67,15 @@
<connections>
<outlet property="mealNameLabel" destination="n9u-9b-TSl" id="6WN-5O-Vth"/>
<outlet property="nameTextField" destination="DYv-F4-28V" id="GJa-MY-QZF"/>
<outlet property="photoImageView" destination="Rhj-yY-68e" id="4Ye-IM-GYI"/>
</connections>
</viewController>
<placeholder placeholderIdentifier="IBFirstResponder" id="dkx-z0-nzr" sceneMemberID="firstResponder"/>
<tapGestureRecognizer id="UAM-fK-D64">
<connections>
<action selector="selectImageFromPhotoLibrary:" destination="BYZ-38-t0r" id="0va-3Q-L6u"/>
</connections>
</tapGestureRecognizer>
</objects>
<point key="canvasLocation" x="-79.200000000000003" y="133.5832083958021"/>
</scene>
......
......@@ -41,5 +41,7 @@
<string>UIInterfaceOrientationLandscapeLeft</string>
<string>UIInterfaceOrientationLandscapeRight</string>
</array>
<key>NSPhotoLibraryUsageDescription</key>
<string>Allows you to add photos to your meals.</string>
</dict>
</plist>
......@@ -7,11 +7,12 @@
import UIKit
class ViewController: UIViewController, UITextFieldDelegate {
class ViewController: UIViewController, UITextFieldDelegate, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
// MARK: Properties
@IBOutlet weak var nameTextField: UITextField!
@IBOutlet weak var mealNameLabel: UILabel!
@IBOutlet weak var photoImageView: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
......@@ -31,7 +32,41 @@ class ViewController: UIViewController, UITextFieldDelegate {
mealNameLabel.text = textField.text
}
//MARK: UIImagePickerControllerDelegate
func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
// Dismiss the picker if the user canceled.
dismiss(animated: true, completion: nil)
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey: Any]) {
// The info dictionary may contain multiple representations of the image. You want to use the original.
guard let selectedImage = info[UIImagePickerController.InfoKey.originalImage] as? UIImage else {
fatalError("Expected a dictionary containing an image, but was provided the following: \(info)")
}
// Set photoImageView to display the selected image.
photoImageView.image = selectedImage
// Dismiss the picker.
dismiss(animated: true, completion: nil)
}
// MARK: Actions
@IBAction func selectImageFromPhotoLibrary(_ sender: UITapGestureRecognizer) {
// Hide the keyboard.
nameTextField.resignFirstResponder()
// UIImagePickerController is a view controller that lets a user pick media from their photo library.
let imagePickerController = UIImagePickerController()
// Only allow photos to be picked, not taken.
imagePickerController.sourceType = .photoLibrary
// Make sure ViewController is notified when the user picks an image.
imagePickerController.delegate = self
present(imagePickerController, animated: true, completion: nil)
}
@IBAction func setDefaultLabelText(_ sender: UIButton) {
mealNameLabel.text = "Default Text"
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册