我是新手,我正在尝试制作一个简单的应用程序,让您拍摄照片,然后在相机上看到它的不透明度。一切似乎都很好,但overlayView放大了,我找不到原因。我该怎么做才能让相机上的overlayView显示整张照片?这是我的代码:
import UIKit
class ViewController: UIViewController,
UIImagePickerControllerDelegate, UINavigationControllerDelegate {
@IBOutlet var pickedImage: UIImageView!
var array = [UIImage]()
var temp = UIImage()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func photoLibrary(_ sender: UIButton) {
if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.photoLibrary){
let imagePicker = UIImagePickerController()
imagePicker.delegate = self
imagePicker.sourceType = UIImagePickerControllerSourceType.photoLibrary;
imagePicker.allowsEditing = true
self.present(imagePicker, animated: true,completion: nil)
}
}
@IBAction func saveButton(_ sender: UIButton) {
let imageData = UIImageJPEGRepresentation( pickedImage.image!, 0.5)
let compressedJPEGImage = UIImage(data: imageData! )
UIImageWriteToSavedPhotosAlbum(compressedJPEGImage!, nil, nil, nil)
array.append(compressedJPEGImage!)
}
@IBAction func cameraButton(_ sender: UIButton) {
if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.camera)
{
let imagePicker = UIImagePickerController()
imagePicker.delegate = self
imagePicker.sourceType = UIImagePickerControllerSourceType.camera
;
imagePicker.allowsEditing = false
self.present(imagePicker, animated: true,completion: nil)
//-------------------------------------------------------------------
// let screenSize: CGRect = UIScreen.main.bounds
let overlayView = UIImageView(frame: imagePicker.cameraOverlayView!.frame)
let crosshairLabel = UIImageView()
if array.count >= 1{
var overlayViewInt = array.count - 1
crosshairLabel.image = array[overlayViewInt]
} else {
crosshairLabel.image = UIImage(named: "camera")
}
overlayView.bounds = UIScreen.main.bounds
crosshairLabel.alpha = 0.4
crosshairLabel.translatesAutoresizingMaskIntoConstraints = false
overlayView.addSubview(crosshairLabel)
crosshairLabel.centerXAnchor.constraint(equalTo: overlayView.centerXAnchor).isActive = true
crosshairLabel.centerYAnchor.constraint(equalTo: overlayView.centerYAnchor).isActive = true
// To avoid blocking the underneath default camera controls
overlayView.isUserInteractionEnabled = false
imagePicker.cameraOverlayView = overlayView
//-------------------------------------------------------------------
}
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingImage image: UIImage!, editingInfo: [NSObject: AnyObject]!) {
pickedImage.image = image
self.dismiss(animated: true, completion: nil)
}
}
答案 0 :(得分:1)
目前还不是很清楚哪个视图应该显示图像,但我认为它是'crosshairLabel'。您需要为其添加宽度和高度约束:
crosshairLabel.heightAnchor.constraint(equalTo: overlayView.heightAnchor, multiplier: 1.0).isActive = true
crosshairLabel.widthAnchor.constraint(equalTo: overlayView.widthAnchor, multiplier: 1.0).isActive = true