我能做到:
let value = array[index];
if (typeof value === 'string') {
value = value.trim();
}
只是好奇是否有更紧凑的方法来做到这一点?
答案 0 :(得分:2)
你不应该需要import UIKit
import AVFoundation
class ViewController: UIViewController, AVCapturePhotoCaptureDelegate {
@IBOutlet var cameraDisplay: UIView!
var captureSession : AVCaptureSession!
var cameraOutput : AVCapturePhotoOutput!
var previewLayer : AVCaptureVideoPreviewLayer!
@IBOutlet var placeImage: UIImageView!
var currentImageView: UIImageView?
override func viewDidLoad() {
super.viewDidLoad()
captureSession = AVCaptureSession()
captureSession.sessionPreset = AVCaptureSessionPresetPhoto
cameraOutput = AVCapturePhotoOutput()
let device = AVCaptureDevice.defaultDevice(withMediaType: AVMediaTypeVideo)
if let input = try? AVCaptureDeviceInput(device: device) {
if (captureSession.canAddInput(input)) {
captureSession.addInput(input)
if (captureSession.canAddOutput(cameraOutput)) {
captureSession.addOutput(cameraOutput)
previewLayer = AVCaptureVideoPreviewLayer(session: captureSession)
previewLayer.frame = CGRect(x: 10, y: 10, width: 300, height: 300)
// cameraDisplay.transform = cameraDisplay.transform.rotated(by: CGFloat(M_PI_2))
cameraDisplay.transform = cameraDisplay.transform.inverted()
cameraDisplay.layer.addSublayer(previewLayer)
captureSession.startRunning()
}
}
}
}
@IBAction func TakePhoto(_ sender: Any) {
let settings = AVCapturePhotoSettings()
let previewpixel = settings.availablePreviewPhotoPixelFormatTypes.first!
let previewformat = [
kCVPixelBufferPixelFormatTypeKey as String: previewpixel, kCVPixelBufferWidthKey as String: 160,
kCVPixelBufferHeightKey as String : 160]
settings.previewPhotoFormat = previewformat
cameraOutput.capturePhoto(with: settings, delegate: self)
}
func image(_ image: UIImage, didFinishSavingWithError error: NSError?, contextInfo: UnsafeRawPointer) {
if let error = error {
let ac = UIAlertController(title: "Save error", message: error.localizedDescription, preferredStyle: .alert)
ac.addAction(UIAlertAction(title: "OK", style: .default))
present(ac, animated: true)
} else {
let ac = UIAlertController(title: "Image Saved!", message: "Your image has been saved to your photos.", preferredStyle: .alert)
ac.addAction(UIAlertAction(title: "OK", style: .default))
present(ac, animated: true)
}
}
func capture(_ captureOutput: AVCapturePhotoOutput, didFinishProcessingPhotoSampleBuffer photoSampleBuffer: CMSampleBuffer?, previewPhotoSampleBuffer: CMSampleBuffer?, resolvedSettings: AVCaptureResolvedPhotoSettings, bracketSettings: AVCaptureBracketedStillImageSettings?, error: Error?) {
if let sampleBuffer = photoSampleBuffer,
let previewBuffer = previewPhotoSampleBuffer,
let dataImage = AVCapturePhotoOutput.jpegPhotoDataRepresentation(forJPEGSampleBuffer: sampleBuffer, previewPhotoSampleBuffer: previewBuffer){
let dataProvider = CGDataProvider(data: dataImage as CFData)
let cgImageRef: CGImage! = CGImage(jpegDataProviderSource: dataProvider!, decode: nil, shouldInterpolate: true, intent: .defaultIntent)
let imagea = UIImage(cgImage: cgImageRef, scale: 1.0, orientation: UIImageOrientation.down)
UIImageWriteToSavedPhotosAlbum(imagea, self, #selector(self.image(_:didFinishSavingWithError:contextInfo:)), nil)
}
}
。你可以做||
;应该足够了。
如果你想以稍微不那么简单的方式做到这一点,那么你可以这么做:
typeof value === 'string'
或三元:
let value = array[index]
value = typeof value === 'string' && value.trim() || value
你拥有它的方式可能还不错。一个好的经验法则是更喜欢更易读但更详细的代码(相对于不太可读但更紧凑的代码)。
@nnnnnn指出了一种更为紧凑的方式:
let value = array[index]
value = typeof value === 'string' ? value.trim() : value