万圣节可以有人帮助我吗?
如何知道从照片库中拍摄的照片模式。
import UIKit
import Photos
class ViewController: UIViewController,UITextFieldDelegate, UIImagePickerControllerDelegate,UINavigationControllerDelegate {
lazy var mealTxtField: paddingTextField = {
let txt = paddingTextField()
txt.placeholder = "Enter meal name"
txt.layer.borderColor = UIColor.orange.cgColor
txt.layer.borderWidth = 1
txt.delegate = self
return txt
}()
var mealNameLbl: UILabel = {
let lbl = UILabel()
lbl.text = "Meal name : "
return lbl
}()
var setLabelBtn:UIButton = {
let btn = UIButton(type: .system)
btn.setTitle("Set defaul lable text", for: .normal)
btn.addTarget(self, action: #selector(setLabel), for: .touchUpInside)
return btn
}()
var mealImage: UIImageView = {
let img = UIImageView()
img.image = UIImage(named: "seafood01")
img.contentMode = .scaleToFill
return img
}()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
view.addSubview(mealNameLbl)
view.addSubview(mealTxtField)
view.addSubview(setLabelBtn)
view.addSubview(mealImage)
//set layout
_ = mealNameLbl.anchor(view.topAnchor, left: view.leftAnchor, bottom: nil, right: nil, topConstant: 64, leftConstant: 20, bottomConstant: 0, rightConstant: 0, widthConstant: 0, heightConstant: 30)
_ = mealTxtField.anchor(mealNameLbl.bottomAnchor, left: view.leftAnchor, bottom: nil, right: view.rightAnchor, topConstant: 8, leftConstant: 20, bottomConstant: 0, rightConstant: 20, widthConstant: 0, heightConstant: 30)
_ = setLabelBtn.anchor(mealTxtField.bottomAnchor, left: view.leftAnchor, bottom: nil, right: nil, topConstant: 8, leftConstant: 20, bottomConstant: 0, rightConstant: 0, widthConstant: 0, heightConstant: 30)
_ = mealImage.anchor(setLabelBtn.bottomAnchor, left: view.leftAnchor, bottom: nil, right: view.rightAnchor, topConstant: 8, leftConstant: 20, bottomConstant: 20, rightConstant: 20, widthConstant: 0, heightConstant: 0)
// some other code
let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(selectImageFromPhotoLibrary(_:)))
mealImage.isUserInteractionEnabled = true
mealImage.addGestureRecognizer(tapGestureRecognizer)
}
func setLabel() {
mealNameLbl.text = "Set to defaul label"
}
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
textField.resignFirstResponder()
return true
}
func textFieldDidEndEditing(_ textField: UITextField) {
mealNameLbl.text = textField.text
}
func selectImageFromPhotoLibrary(_ sender: UITapGestureRecognizer) {
mealTxtField.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)
}
func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
dismiss(animated: true, completion: nil)
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
guard let selectedImage = info[UIImagePickerControllerOriginalImage] as? UIImage else {
fatalError("Expected a dictionary containing an image, but was provided the following: \(info)")
}
mealImage.image = selectedImage
dismiss(animated: true, completion: nil)
}