我正在尝试将sampleBuffer转换为UIImage并使用colorspaceGray在图像视图中显示它.但它显示为以下图像.这个问题出了什么问题?如何转换CMSampleBuffer?
func captureOutput(_ captureOutput: AVCaptureOutput!, didOutputSampleBuffer sampleBuffer: CMSampleBuffer!, from connection: AVCaptureConnection!) {
print("buffered")
let imageBuffer: CVImageBuffer = CMSampleBufferGetImageBuffer(sampleBuffer)!
CVPixelBufferLockBaseAddress(imageBuffer, CVPixelBufferLockFlags(rawValue: 0))
let width: Int = CVPixelBufferGetWidth(imageBuffer)
let height: Int = CVPixelBufferGetHeight(imageBuffer)
let bytesPerRow: Int = CVPixelBufferGetBytesPerRow(imageBuffer)
let lumaBuffer = CVPixelBufferGetBaseAddress(imageBuffer)
//let planeCount : Int = CVPixelBufferGetPlaneCount(imageBuffer)
let grayColorSpace: CGColorSpace = CGColorSpaceCreateDeviceGray()
let context: CGContext = CGContext(data: lumaBuffer, width: width, height: height, bitsPerComponent: 8, bytesPerRow: bytesPerRow , space: grayColorSpace, bitmapInfo: CGImageAlphaInfo.none.rawValue)!
let dstImageFilter: CGImage = context.makeImage()!
let imageRect : CGRect = …Run Code Online (Sandbox Code Playgroud) 我尝试使用 swiftUI 实现一个滚动视图。在滚动视图中有一个包含另一个视图的水平堆栈(HStack)。我希望滚动视图水平滚动。但它也垂直滚动。以下是我的代码:
struct categoryRow : View {
var categoryName: String
var landmarks : [Landmark]
var body: some View {
VStack(alignment: .leading){
Text(categoryName).font(.headline).padding(EdgeInsets(top: 15, leading: 10, bottom: 0, trailing: 0))
ScrollView (alwaysBounceHorizontal: true, showsHorizontalIndicator: false, showsVerticalIndicator: false ){
HStack(spacing: 15){
ForEach(landmarks) { landmark in
NavigationButton(destination: LandMarkDetails(landmark: landmark)) {
CategoryItem(landmark: landmark)
}
}
}.padding()
}.frame( height: 190)
}
}
}
Run Code Online (Sandbox Code Playgroud)
我还没有找到任何选项来停止垂直滚动。我在这里缺少什么?
我建立了一个相机应用程序来自动捕获。我想在打开相机的同时一直保持闪光灯打开。我设置以下代码:
cameraDevice = AVCaptureDevice.defaultDevice(withMediaType: AVMediaTypeVideo)
if (cameraDevice.hasTorch) {
do {
try cameraDevice.lockForConfiguration()
if cameraDevice.isTorchActive {
cameraDevice.torchMode = AVCaptureTorchMode.on
} else {
// sets the torch intensity to 100%
try cameraDevice.setTorchModeOnWithLevel(0.8)
}
cameraDevice.unlockForConfiguration()
} catch {
print(error)
}
}
Run Code Online (Sandbox Code Playgroud)
但是,当我运行该应用程序时,它只会闪烁一次,然后熄灭。我怎么解决这个问题?
我正在尝试将 UIImage 转换为灰度。它所产生的只是一个完全黑色的图像。
func convertToGrayScale2(image: UIImage) -> UIImage {
let imageRect:CGRect = CGRect(x:0, y:0, width:image.size.width, height: image.size.height)
let colorSpace = CGColorSpaceCreateDeviceGray()
let width = image.size.width
let height = image.size.height
let bitmapInfo = CGBitmapInfo(rawValue: CGImageAlphaInfo.none.rawValue)
let context = CGContext(data: nil, width: Int(width), height: Int(height), bitsPerComponent: 8, bytesPerRow: 0, space: colorSpace, bitmapInfo: bitmapInfo.rawValue)
let imageRef = context!.makeImage()
context?.draw(imageRef!, in: imageRect)
let newImage = UIImage(cgImage: imageRef!)
return newImage
}
Run Code Online (Sandbox Code Playgroud)
代码有什么问题吗?我需要现有代码的帮助。它之前正在工作。突然它不工作了。