以下是后置摄像头的代码,我尝试添加后置摄像头已激活,但收到错误。
func addImagePickerToContainerView(){
imagePickers = UIImagePickerController()
if UIImagePickerController.isCameraDeviceAvailable( UIImagePickerControllerCameraDevice.front) {
imagePickers?.delegate = self
imagePickers?.sourceType = UIImagePickerControllerSourceType.camera
imagePickers?.cameraDevice = .rear
addChildViewController(imagePickers!)
// Add the child's View as a subview
self.cameraContainerView.addSubview((imagePickers?.view)!)
self.cameraContainerView.addSubview(layerKTP)
imagePickers?.view.frame = cameraContainerView.bounds
imagePickers?.allowsEditing = false
imagePickers?.showsCameraControls = false
imagePickers?.view.autoresizingMask = [.flexibleWidth, .flexibleHeight]
}
}
来自调试器的消息:由于内存问题而终止
最佳答案
尝试以下代码
extension UIImage
{
func resizeImage(toSize size:CGSize, scale:CGFloat) ->UIImage
{
let imageRect = CGRect(origin: CGPoint(x:0.0, y:0.0), size: size)
UIGraphicsBeginImageContextWithOptions(size, false, scale)
self.draw(in: imageRect)
let resizedImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return resizedImage!
}
}
//Use this method like
let resizedImage = selectedImg.resizeImage(toSize:CGSize(width:100.0, height:100.0), scale: UIScreen.main.scale)