- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
在 Swift 3 中工作。我发现了很多有答案的问题,还有博客,但我尝试过的一切都没有奏效。我只是想捕捉我拍摄的相机照片并将其保存到文档中。但它没有被保存,因为当从 xcode 中查看时它没有显示在设备文档下,而且我没有收到任何错误或类似错误。我有点迷路了。
获取图片并保存的代码
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
var pickedImage = UIImage()
pickedImage = info[UIImagePickerControllerOriginalImage] as! UIImage
picker.dismiss(animated: true, completion: nil)
let currentDateTime = Date()
let formatter = DateFormatter()
formatter.dateFormat = "yyyyMMddHHmmss"
let fileNameWithExtension = "ts_\(formatter.string(from: currentDateTime)).png"
//create path
let imagePath = fileInDocumentsDirectory(filename: fileNameWithExtension)
imageStringPathSet = fileNameWithExtension
imageSet = pickedImage
if saveImage(image: pickedImage, path: imagePath) {
cameraButton.setImage(#imageLiteral(resourceName: "ic_camerashot_yes60dp"), for: UIControlState.normal)
return
}
cameraButton.setImage(#imageLiteral(resourceName: "ic_camerashot_no60dp"), for: UIControlState.normal)
}
func fileInDocumentsDirectory(filename: String)-> URL {
return try! FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: false).appendingPathComponent(filename)
}
func saveImage(image: UIImage, path: URL) -> Bool {
print(path)
guard let pngImageData = UIImagePNGRepresentation(image) else {
print("error")
return false
}
var resultValid = false
do {
let results = try pngImageData.write(to: path, options: [.atomicWrite])
print(results) //prints ()
resultValid = true
}
catch {
resultValid = false
print(error)
}
return resultValid
}
当我打印它打印的路径时
file:///var/mobile/Containers/Data/Application/01EB6A70-34C6-4481-BE5B-7F7AB5E6703F/Documents/ts_20161221145652.png
我认为这是正确的。如果一切正常,它会改变屏幕上的图像,但它永远不会改变,并且 imageStringPathSet 也没有设置,这是一个类变量。任何人都知道我需要做什么才能让它发挥作用?
解决方案
原来原因是我在 viewWillAppear 中重置了 View 中的所有内容。解决此问题后,一切正常。感谢大家的反馈。希望这可以帮助其他人不要做我所做的事情。
最佳答案
根据评论中的要求,这是我的代码,(a) 使用 UIImagePickerController 从相机胶卷中选择,然后 (b) 使用 UIActivityViewController 让用户选择几种可能的方式来保存/附加图像到多个输出源。
虽然这不会保存到文档目录,但它可能是更好的选择。请注意代码 list 后的一些注释。
我的“选择” View Controller ,允许用户从相机胶卷中选择或拍照:
extension SelectViewController: UIImagePickerControllerDelegate, UINavigationControllerDelegate {
// MARK: Camera App
func openCameraApp() {
if UIImagePickerController.availableCaptureModes(for: .rear) != nil {
picker.allowsEditing = false
picker.sourceType = UIImagePickerControllerSourceType.camera
picker.cameraCaptureMode = .photo
picker.modalPresentationStyle = .fullScreen
present(picker,
animated: true,
completion: nil)
} else {
noCamera()
}
}
func noCamera(){
let alertVC = UIAlertController(
title: "No Camera",
message: "Sorry, this device has no camera",
preferredStyle: .alert)
let okAction = UIAlertAction(
title: "OK",
style:.default,
handler: nil)
alertVC.addAction(okAction)
present(
alertVC,
animated: true,
completion: nil)
}
// MARK: Photos Albums
func showImagePicker() {
picker.allowsEditing = false
picker.sourceType = .photoLibrary
// picker.modalPresentationStyle = .Popover
present(picker,
animated: true,
completion: nil)
picker.popoverPresentationController?.sourceView = self.view
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
let chosenImage = info[UIImagePickerControllerOriginalImage] as! UIImage
image = chosenImage
self.performSegue(withIdentifier: "ShowEditView", sender: self)
dismiss(animated: true, completion: nil)
}
func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
dismiss(animated: false, completion: nil)
}
// MARK: Seque to EditViewController
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "ShowEditView" {
if let vc = segue.destination as? EditViewController {
vc.image = image
}
}
}
}
我的“编辑” View Controller 嵌入在 UINavigationBar 中,上面有一个按钮设置为 System Item == Action:
// MARK: Image actions
@IBAction func shareImage(_ sender: UIBarButtonItem) {
let context = CIContext()
let final = context.createCGImage(imgEdited, from: imgEdited.extent)
let shareImage = UIImage(cgImage: final!)
let vc = UIActivityViewController(activityItems: [shareImage], applicationActivities: [])
vc.excludedActivityTypes = [
//UIActivityTypePostToTwitter,
//UIActivityTypePostToFacebook,
UIActivityType.postToWeibo,
//UIActivityTypeMessage,
//UIActivityTypeMail,
UIActivityType.print,
//UIActivityTypeCopyToPasteboard,
UIActivityType.assignToContact,
//UIActivityTypeSaveToCameraRoll,
UIActivityType.addToReadingList,
//UIActivityTypePostToFlickr,
UIActivityType.postToVimeo,
UIActivityType.postToTencentWeibo
]
present(vc,
animated: true,
completion: nil)
vc.popoverPresentationController?.sourceView = self.view
vc.completionWithItemsHandler = {(activity, success, items, error) in
}
}
注意事项:
请记住,在 iOS 10 设备中使用 UIImagePickerController 时,它会崩溃,除非您将它添加到您的 info.plist:
<key>NSCameraUsageDescription</key>
<string>Used to capture new image for photo effect</string>
<key>NSPhotoLibraryUsageDescription</key>
<string>Used to select an image for photo effect</string>
您可以在标签中使用您想要的任何内容。
UIActivityViewController 将在其excludedActivityTypes 中显示所有已注释 的选项。 (我列出这些选项只是为了 self 记录。)如果用户有 Facebook 并想在上面发帖,如果他们还没有登录,系统会提示他们登录。
关于ios - Swift 3 拍照并保存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41271943/
所以我对 Cordova 还很陌生,现在我正在使用它来制作一个应用程序,我正在尝试拍照然后保存它。现在我还不能拍照。我所拥有的只是一个按钮,在相应的 javascript 文件中,按下按钮时会调用一个
我有完整的工作代码来拍照、裁剪、发送带有图像附件的电子邮件。 代码流程如下:拍张照片 > 找到并选择我们刚刚从图库中拍摄的照片> 裁剪它 > 通过电子邮件发送附件。 我想了解如何跳过整个“查找并选择我
我是编程新手,正在尝试使用板载摄像头硬件制作应用程序,我的目的是拍照;然后,当您单击“保存”时,该图片将出现在要编辑的新 Activity 中...我已经研究了几天如何最好地使用相机硬件...有人告诉
有没有一种方法可以使用 UIImagePickerController 中的叠加层来显示用户可能使用的方形图片,同时在某处有一个切换按钮可以即时切换? 目前 iOS 7 相机有这个功能,但是 UIIm
当我点击 GestureDetector 时,它会打开一个对话框,我可以在其中选择是要从图库中拍摄照片还是要自己制作照片 (the plugin)。但我的问题是当我点击对话框中的一个按钮时,对话框再次
我的一个页面上有一个 map 控件,我想拍一张照片并将其显示在不同的屏幕上。有谁知道我怎么能做到这一点? map 在 内. 最佳答案 您应该使用 WriteableBitmap 类的 Render
我一直在构建一个使用 Flurge 的 CameraKit 的项目,但我遇到了一个相当烦人的问题。其实我不知道怎么拍照。我有一个快门按钮,按下该按钮应该会激活 OnPicturetaken Liste
当我启动照片捕捉 Intent 时,返回给我的照片路径是:content://media/external/images/media/40209 但是当我查看我的设备时,照片路径应该类似于 [..]/
我结账Camera2Basic (谷歌样本) 我刚刚在类 Camera2BasicFragment 中更改了这个方法为了用前置摄像头拍照: private void setUpCameraOutp
在 fragment 中我想拍照但是我遇到了问题,我从来没有得到 onActivityResult 的回调 我的代码: private void dispatchTakePictureIntent(
当我使用 UIImagePicker 从相机拍照时,背景音乐(来自 ipod 应用程序、pandora 应用程序)停止。 Facebook 应用程序不会发生这种情况。有单独的代表来做这件事吗?请帮忙
我用这个方法拍了一张照片。 func convertImageFromCMSampleBufferRef(sampleBuffer:CMSampleBuffer) -> CIImage{ le
在我的 chrome 上,以下代码仅打开相机并立即崩溃,即显示黑屏。这段代码在 Firefox 上运行良好。为什么? http://jsfiddle.net/2mLb6cs2/ (function()
我正在尝试在我的 iOS 应用程序中实现 OpenCV。 CvVideoCamera 很棒,因为它有一个委托(delegate)方法,可以让我处理相机预览的每一帧,但我不知道如何提取一帧并将其保存为图
当我想使用相机拍照时,CameraSource 出现了一些问题。我想要的只是用我在 xml 文件上制作的按钮拍照并使用“CameraSource”拍照,因为我也在使用“TextRecognizer”。
可以禁用/删除此照片确认对话框: 我需要以某种方式跳过此对话框,但我仍想使用 Intent。我找到了这个 android: Take camera picture without "save" / "
我正在使用 FileProvider 在 Android Nougat 上拍照,这是我的代码 文件路径.xml: Java: String fileName =
将文件路径额外添加到图像捕获 Intent 会导致相机应用程序在 TF300t Android 平板电脑上出现故障,系统版本为 4.2.1。按“完成”按钮什么都不做——甚至不关闭相机应用程序 Acti
我正在构建一个将跟踪库存的网络内联网应用程序,我希望能够使用 iPad 的内置相机为每件元素拍照。这可能吗? 我已经使用应用对条形码做了类似的事情,但我还没有找到任何可以对照片做同样事情的东西。 最佳
我用这个方法拍了一张照片。 func convertImageFromCMSampleBufferRef(sampleBuffer:CMSampleBuffer) -> CIImage{ le
我是一名优秀的程序员,十分优秀!