我正在尝试使用 AVFoundation 使用相同的 AVCaptureSession 制作一个包含音频、视频和照片的应用程序。
以下是我如何设置相机和捕获会话。
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 | captureSession.sessionPreset = AVCaptureSession.Preset.photo } func setUpDevice(){ let deviceDiscoverSession = AVCaptureDevice.DiscoverySession(deviceTypes: [AVCaptureDevice.DeviceType.builtInWideAngleCamera], mediaType: AVMediaType.video, position: AVCaptureDevice.Position.unspecified) let devices = deviceDiscoverSession.devices for device in devices{ if device.position == AVCaptureDevice.Position.back{ backCamera = device }else{ frontCamera = device } } currentCamera = backCamera } func setUpInputOutput(){ do{ let captureDeviceInput = try AVCaptureDeviceInput(device: currentCamera!) captureSession.addInput(captureDeviceInput) photoOutput = AVCapturePhotoOutput() photoOutput?.isHighResolutionCaptureEnabled = true self.captureSession.addOutput(photoOutput!) if #available(iOS 11.0, *) { photoOutput?.setPreparedPhotoSettingsArray([AVCapturePhotoSettings(format: [AVVideoCodecKey : AVVideoCodecType.jpeg])], completionHandler: nil) } else { // Fallback on earlier versions photoOutput?.setPreparedPhotoSettingsArray([AVCapturePhotoSettings(format: [AVVideoCodecKey: AVVideoCodecJPEG])], completionHandler: nil) } }catch{ print(error) } } func setUpPreviewLayer(){ cameraPreviewLayer = AVCaptureVideoPreviewLayer(session: captureSession) cameraPreviewLayer?.videoGravity = AVLayerVideoGravity.resizeAspectFill cameraPreviewLayer?.connection?.videoOrientation = AVCaptureVideoOrientation.portrait cameraPreviewLayer?.frame = self.cameraView.frame self.cameraView.layer.insertSublayer(cameraPreviewLayer!, at: 0) } func startRunningCaptureSession(){ captureSession.startRunning() //Session starts with camera as default so set it to gesture let doubleTap = UITapGestureRecognizer(target: self, action: #selector(takePhoto(sender:))) doubleTap.numberOfTapsRequired = 2 self.view.addGestureRecognizer(doubleTap) } |
要从相机拍照(正在工作,照片存储在图库中),我做了以下操作:
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 | if(imageSetAction()){ cameraImage.image = UIImage(named:"photo") cameraLabel.textColor = ConstantColors.selectedTextColor currentSelected = sender.tag let doubleTap = UITapGestureRecognizer(target: self, action: #selector(takePhoto(sender:))) doubleTap.numberOfTapsRequired = 2 self.view.addGestureRecognizer(doubleTap) } } @objc func takePhoto(sender: Any){ guard let capturePhotoOutput = self.photoOutput else { return } let photoSettings = AVCapturePhotoSettings() photoSettings.isAutoStillImageStabilizationEnabled = true photoSettings.isHighResolutionPhotoEnabled = true photoSettings.flashMode = .auto capturePhotoOutput.capturePhoto(with: photoSettings, delegate: self) } func photoOutput(_ captureOutput: AVCapturePhotoOutput, didFinishProcessingPhoto photoSampleBuffer: CMSampleBuffer?, previewPhoto previewPhotoSampleBuffer: CMSampleBuffer?, resolvedSettings: AVCaptureResolvedPhotoSettings, bracketSettings: AVCaptureBracketedStillImageSettings?, error: Error?) { guard error == nil, let photoSampleBuffer = photoSampleBuffer else { print("Error capturing photo: \\(String(describing: error))") return } guard let imageData = AVCapturePhotoOutput.jpegPhotoDataRepresentation(forJPEGSampleBuffer: photoSampleBuffer, previewPhotoSampleBuffer: previewPhotoSampleBuffer) else { return } let capturedImage = UIImage.init(data: imageData , scale: 1.0) if let image = capturedImage { //UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil) let vc = UIStoryboard.init(name:"ImageEdit", bundle: Bundle.main).instantiateViewController(withIdentifier:"imageEdit") as? ImageEdit vc?.imagetoEdit = image self.navigationController?.pushViewController(vc!, animated: true) } } |
但是当我尝试从同一个捕获会话中拍照时,我收到错误 "NSInvalidArgumentException\\',原因:\\'*** -[AVCaptureMovieFileOutput startRecordingToOutputFileURL:recordingDelegate:] No active/enabled connections\\'"。下面是我尝试做的代码。
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 | print("completed") } @objc func videoAction(sender: UIButton){ if(imageSetAction()){ videoImage.image = UIImage(named:"video") videoLabel.textColor = ConstantColors.selectedTextColor currentSelected = sender.tag captureSession.removeOutput(photoOutput!) self.movieFileOutput = AVCaptureMovieFileOutput() self.captureSession.addOutput(movieFileOutput!) let longPressGesture = UILongPressGestureRecognizer.init(target: self, action: #selector(handleLongPress)) self.view.addGestureRecognizer(longPressGesture); } } @objc func handleLongPress(gestureRecognizer: UILongPressGestureRecognizer) { if gestureRecognizer.state == UIGestureRecognizerState.began { debugPrint("long press started") let documentsURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0] as URL let filePath = documentsURL.appendingPathComponent("tempMovie.mp4") if FileManager.default.fileExists(atPath: filePath.absoluteString) { do { try FileManager.default.removeItem(at: filePath) } catch { // exception while deleting old cached file // ignore error if any } } movieFileOutput?.startRecording(to: filePath, recordingDelegate: self) } else if gestureRecognizer.state == UIGestureRecognizerState.ended { debugPrint("longpress ended") movieFileOutput?.stopRecording() } } |
对此的任何帮助表示赞赏。
您可以尝试像这样更改 videoAction 中的预设
你可以在这里看到类似的问题
AVCaptureMovieFileOutput - 没有活动/启用的连接
使用带有Rails插件的vim,您可以创建一个迁移文件,然后一次性打开该文件吗?textmate也可以这样吗? 最佳答案 你可以使用rails.vim然后做类似的事情::Rgeneratemigratonadd_foo_to_bar插件将打开迁移生成的文件,这正是您想要的。我不能代表textmate。 关于ruby-使用VimRails,您可以创建一个新的迁移文件并一次性打开它吗?,我们在StackOverflow上找到一个类似的问题: https://sta
我需要从一个View访问多个模型。以前,我的links_controller仅用于提供以不同方式排序的链接资源。现在我想包括一个部分(我假设)显示按分数排序的顶级用户(@users=User.all.sort_by(&:score))我知道我可以将此代码插入每个链接操作并从View访问它,但这似乎不是“ruby方式”,我将需要在不久的将来访问更多模型。这可能会变得很脏,是否有针对这种情况的任何技术?注意事项:我认为我的应用程序正朝着单一格式和动态页面内容的方向发展,本质上是一个典型的网络应用程序。我知道before_filter但考虑到我希望应用程序进入的方向,这似乎很麻烦。最终从任何
我想要做的是有2个不同的Controller,client和test_client。客户端Controller已经构建,我想创建一个test_clientController,我可以使用它来玩弄客户端的UI并根据需要进行调整。我主要是想绕过我在客户端中内置的验证及其对加载数据的管理Controller的依赖。所以我希望test_clientController加载示例数据集,然后呈现客户端Controller的索引View,以便我可以调整客户端UI。就是这样。我在test_clients索引方法中试过这个:classTestClientdefindexrender:template=>
我构建了两个需要相互通信和发送文件的Rails应用程序。例如,一个Rails应用程序会发送请求以查看其他应用程序数据库中的表。然后另一个应用程序将呈现该表的json并将其发回。我还希望一个应用程序将存储在其公共(public)目录中的文本文件发送到另一个应用程序的公共(public)目录。我从来没有做过这样的事情,所以我什至不知道从哪里开始。任何帮助,将不胜感激。谢谢! 最佳答案 无论Rails是什么,几乎所有Web应用程序都有您的要求,大多数现代Web应用程序都需要相互通信。但是有一个小小的理解需要你坚持下去,网站不应直接访问彼此
我尝试运行2.x应用程序。我使用rvm并为此应用程序设置其他版本的ruby:$rvmuseree-1.8.7-head我尝试运行服务器,然后出现很多错误:$script/serverNOTE:Gem.source_indexisdeprecated,useSpecification.Itwillberemovedonorafter2011-11-01.Gem.source_indexcalledfrom/Users/serg/rails_projects_terminal/work_proj/spohelp/config/../vendor/rails/railties/lib/r
设置:狂欢ruby1.9.2高线(1.6.13)描述:我已经相当习惯在其他一些项目中使用highline,但已经有几个月没有使用它了。现在,在Ruby1.9.2上全新安装时,它似乎不允许在同一行回答提示。所以以前我会看到类似的东西:require"highline/import"ask"Whatisyourfavoritecolor?"并得到:Whatisyourfavoritecolor?|现在我看到类似的东西:Whatisyourfavoritecolor?|竖线(|)符号是我的终端光标。知道为什么会发生这种变化吗? 最佳答案
刚入门rails,开始慢慢理解。有人可以解释或给我一些关于在application_controller中编码的好处或时间和原因的想法吗?有哪些用例。您如何为Rails应用程序使用应用程序Controller?我不想在那里放太多代码,因为据我了解,每个请求都会调用此Controller。这是真的? 最佳答案 ApplicationController实际上是您应用程序中的每个其他Controller都将从中继承的类(尽管这不是强制性的)。我同意不要用太多代码弄乱它并保持干净整洁的态度,尽管在某些情况下ApplicationContr
如果您尝试在Ruby中的nil对象上调用方法,则会出现NoMethodError异常并显示消息:"undefinedmethod‘...’fornil:NilClass"然而,有一个tryRails中的方法,如果它被发送到一个nil对象,它只返回nil:require'rubygems'require'active_support/all'nil.try(:nonexisting_method)#noNoMethodErrorexceptionanymore那么try如何在内部工作以防止该异常? 最佳答案 像Ruby中的所有其他对象
关闭。这个问题需要detailsorclarity.它目前不接受答案。想改进这个问题吗?通过editingthispost添加细节并澄清问题.关闭8年前。Improvethisquestion为什么SecureRandom.uuid创建一个唯一的字符串?SecureRandom.uuid#=>"35cb4e30-54e1-49f9-b5ce-4134799eb2c0"SecureRandom.uuid方法创建的字符串从不重复?
我是一个Rails初学者,但我想从我的RailsView(html.haml文件)中查看Ruby变量的内容。我试图在ruby中打印出变量(认为它会在终端中出现),但没有得到任何结果。有什么建议吗?我知道Rails调试器,但更喜欢使用inspect来打印我的变量。 最佳答案 您可以在View中使用puts方法将信息输出到服务器控制台。您应该能够在View中的任何位置使用Haml执行以下操作:-puts@my_variable.inspect 关于ruby-on-rails-如何在我的R