- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在做一些我认为不应该那么困难的事情上遇到了困难,所以我想我一定是从错误的角度看待问题。为了了解 AVCaptureStillImageOutput 和相机的工作原理,我制作了一个小应用程序。
这个应用程序能够拍摄照片并将其保存为 PNG 文件(我不需要 JPEG)。下次启动应用程序时,它会检查文件是否存在,如果存在,则将文件中存储的图像用作应用程序的背景 View 。这个想法相当简单。
问题是它不起作用。如果有人能告诉我我做错了什么,那将会非常有帮助。
我希望图片以拍摄时在显示屏上的方式显示为背景,但它被旋转或比例错误......等等。
这是相关代码(如果需要,我可以提供更多信息)。
viewDidLoad
方法:
override func viewDidLoad() {
super.viewDidLoad()
// For the photo capture:
captureSession.sessionPreset = AVCaptureSessionPresetHigh
// Select the appropriate capture devices:
for device in AVCaptureDevice.devices() {
// Make sure this particular device supports video.
if (device.hasMediaType(AVMediaTypeVideo)) {
// Finally check the position and confirm we've got the back camera.
if(device.position == AVCaptureDevicePosition.Back) {
captureDevice = device as? AVCaptureDevice
}
}
}
tapGesture = UITapGestureRecognizer(target: self, action: Selector("takePhoto"))
self.view.addGestureRecognizer(tapGesture)
let filePath = self.toolBox.getDocumentsDirectory().stringByAppendingPathComponent("BackGroundImage@2x.png")
if !NSFileManager.defaultManager().fileExistsAtPath(filePath) {return}
let bgImage = UIImage(contentsOfFile: filePath),
bgView = UIImageView(image: bgImage)
self.view.addSubview(bgView)
}
处理拍照的方法:
func takePhoto() {
if !captureSession.running {
beginPhotoCaptureSession()
return
}
if let videoConnection = stillImageOutput.connectionWithMediaType(AVMediaTypeVideo) {
stillImageOutput.captureStillImageAsynchronouslyFromConnection(videoConnection) {
(imageDataSampleBuffer, error) -> Void in
if error == nil {
var localImage = UIImage(fromSampleBuffer: imageDataSampleBuffer)
UIGraphicsBeginImageContext(localImage!.size)
CGContextRotateCTM (UIGraphicsGetCurrentContext(), CGFloat(M_PI_2))
//localImage!.drawAtPoint(CGPointZero)
localImage!.drawAtPoint(CGPoint(x: -localImage!.size.height, y: -localImage!.size.width))
//localImage!.drawAtPoint(CGPoint(x: -localImage!.size.width, y: -localImage!.size.height))
localImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
localImage = resizeImage(localImage!, toSize: self.view.frame.size)
if let data = UIImagePNGRepresentation(localImage!) {
let bitMapName = "BackGroundImage@2x"
let filename = self.toolBox.getDocumentsDirectory().stringByAppendingPathComponent("\(bitMapName).png")
data.writeToFile(filename, atomically: true)
print("Picture saved: \(bitMapName)\n\(filename)")
}
} else {print("Error on taking a picture:\n\(error)")}
}
}
captureSession.stopRunning()
previewLayer!.removeFromSuperlayer()
}
启动AVCaptureSession的方法:
func beginPhotoCaptureSession() {
do {let input = try AVCaptureDeviceInput(device: captureDevice)
captureSession.addInput(input)
} catch let error as NSError {
// Handle any errors:
print(error)
}
previewLayer = AVCaptureVideoPreviewLayer(session: captureSession)
previewLayer?.frame = self.view.layer.frame
self.view.layer.addSublayer(previewLayer!)
captureSession.startRunning()
stillImageOutput.outputSettings = [kCVPixelBufferPixelFormatTypeKey:Int(kCVPixelFormatType_32BGRA)]
if captureSession.canAddOutput(stillImageOutput) {
captureSession.addOutput(stillImageOutput)
}
}
以下是使用该应用拍摄的照片的示例:
现在这是我重新启动应用程序时得到的背景:
如果工作正常,两张图片将会相似。
最佳答案
我在屏幕截图中看不到旋转。但是比例是一个问题,当您在 function takePhoto
中绘制它时,它与您的代码有关。你可以试试
func takePhoto() {
if !captureSession.running {
beginPhotoCaptureSession()
return
}
if let videoConnection = stillImageOutput.connectionWithMediaType(AVMediaTypeVideo) {
stillImageOutput.captureStillImageAsynchronouslyFromConnection(videoConnection) {
(imageDataSampleBuffer, error) -> Void in
if error == nil {
if let data = AVCaptureStillImageOutput.jpegStillImageNSDataRepresentation(imageDataSampleBuffer) {
let bitMapName = "BackGroundImage@2x"
let filename = self.toolBox.getDocumentsDirectory().stringByAppendingPathComponent("\(bitMapName).png")
data.writeToFile(filename, atomically: true)
print("Picture saved: \(bitMapName)\n\(filename)")
}
} else {print("Error on taking a picture:\n\(error)")}
}
}
captureSession.stopRunning()
previewLayer!.removeFromSuperlayer()
}
关于ios - AVCaptureStillImageOutput 和 UIImagePNGRepresentation,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34646982/
我有一些透明图像,正在从文件系统加载到 UIImageView View 中。出于我的目的,我需要将 UIImageView 中的图像与文件系统上的文件进行比较。所以我做了如下的事情: NSStrin
我正在开发一个使用相机拍照的 iPhone 应用程序,然后将它们保存到应用程序文档目录中。我使用以下代码将 UIImage 转换为 NSData, NSData *imageData = [NSDat
我发现这个模块很麻烦。我从 Photolibrary 导入了 100 多张图片,将它们保存在不同名称的文档目录中。不出所料,我在不寻常的地方遇到了内存问题。似乎 UIImagePNGRepresena
使用此代码,我从共享扩展中提取一张图像,并将其写入我在应用程序组中创建的目录。 let content = self.extensionContext!.inputItems[0] as! NSExt
我意识到之前有人问过这个问题,但我没有找到任何真正解决它的答案,所以......再问一次。 我正在使用 ARC。我的应用程序定期从 AVCaptureSession 中拍摄照片并将它们保存到核心数据中
我正在开发一个使用网络协议(protocol) UPnP 连接多个设备的应用程序。 除其他外,我可以交换图像,它们是存储在我的项目文件夹中的文件夹中的文件。 为了发送图像,我得到了预定义的方法。 if
如果我们使用 UIImagePNGRepresentation 转换 JPEG 文件,那么新文件的大小会更大。如果我们使用 UIImagePNGRepresentation 转换 PNG 文件,那么新
我正在尝试将图像从 UIImagePicker 复制到文档目录。我正在使用 @"UIImagePickerControllerOriginalImage" 键从 UIImagePickerDelega
我正在尝试制作缩略图并保存到文档目录。但问题是当我试图将缩略图图像转换为 NSData 时。它返回零。 这是我的代码, UIImage *thumbNailimage=[image thumbna
每次我使用相机拍照,然后保存时,图像总是横向的。这意味着我的 Xib 中的 UIImageView 是错误的。它的肖像 - 这就是我想要和期望的。我可以通过将图像旋转 90 度来纠正此问题,但即使如此
我在做一些我认为不应该那么困难的事情上遇到了困难,所以我想我一定是从错误的角度看待问题。为了了解 AVCaptureStillImageOutput 和相机的工作原理,我制作了一个小应用程序。 这个应
我在我的代码中使用了下面一行 NSData *dataForImage=UIImagePNGRepresentation(image); 我的应用程序因内存不足而崩溃。当我使用 VM 跟踪器/分配
我有一个应该代表复选框的按钮。单击按钮时,imageView.image-Property 将设置为另一个 UIImage。因此,我有两个变量用于已检查和未检查图像。 UIImage *checked
图片来自UIImagePickerViewController: func imagePickerController(picker: UIImagePickerController, didFini
我的 iphone 照片应用程序中有图像文件。 我已经通过空投将它带到我的 macbook 上,并检查了图像的大小 = 1.9 MB 我将相同的图像保存到 iphone 的文件应用程序并检查了那里的图
我将新收到的 PDF 文档的缩略图呈现到文档目录中。 我正在使用以下代码: CFURLRef pdfURL = CFBundleCopyResourceURL(CFBundleGetMainBundl
使用 Swift 我尝试从 URL 下载 JPG 然后将该图像保存到文件,但是当我尝试将它保存到不同的目录时它惯于。它将下载到应用程序的 Documents 文件夹,但当我尝试将路径设置为另一个子文件
我正在尝试下载一些图像(从 base64 字符串转换为 UIImage)并将其保存到设备,但不断收到内存警告。 ontracFullScreenImageViewController *etrackD
这就是我从 UIImage 创建 Data 的方式: let data = UIImagePNGRepresentation(image) 然后我需要将它转换成String; if let d
代码 static func encodeImage(image: UIImage) -> String{ var imageData = UIImagePNGRepresentation(i
我是一名优秀的程序员,十分优秀!