我正在尝试将仅使用Objective-C编写的Apple自己的示例代码转换为Swift。
这就是Objective C
中的内容·H
@import UIKit;
@class AVCaptureSession;
@interface AAPLPreviewView : UIView
@property (nonatomic) AVCaptureSession *session;
@end
的.m
@import AVFoundation;
#import "AAPLPreviewView.h"
@implementation AAPLPreviewView
+ (Class)layerClass
{
return [AVCaptureVideoPreviewLayer class];
}
- (AVCaptureSession *)session
{
AVCaptureVideoPreviewLayer *previewLayer = (AVCaptureVideoPreviewLayer *)self.layer;
return previewLayer.session;
}
- (void)setSession:(AVCaptureSession *)session
{
AVCaptureVideoPreviewLayer *previewLayer = (AVCaptureVideoPreviewLayer *)self.layer;
previewLayer.session = session;
}
@end
到目前为止,这是我为Swift提出的:
import AVFoundation
import UIKit
class CamPreviewLayer: UIView
{
var session: AVCaptureSession!
override class func layerClass() -> AnyClass
{
return AVCaptureVideoPreviewLayer
}
func session() -> AVCaptureSession {
var previewLayer: AVCaptureVideoPreviewLayer = self.layer as! AVCaptureVideoPreviewLayer
return previewLayer.session
}
func setSession(session: AVCaptureSession) {
let previewLayer: AVCaptureVideoPreviewLayer = self.layer as! AVCaptureVideoPreviewLayer
previewLayer.session = session
}
}
我的Swift代码有3个错误。
答案 0 :(得分:1)
您必须像这样删除var session: AVCaptureSession
。
import AVFoundation
import UIKit
class AVPreviewView : UIView {
override class func layerClass() -> AnyClass {
return AVCaptureVideoPreviewLayer.self
}
func session () -> AVCaptureSession {
return (self.layer as! AVCaptureVideoPreviewLayer).session
}
func setSession(session : AVCaptureSession) -> Void {
(self.layer as! AVCaptureVideoPreviewLayer).session = session;
(self.layer as! AVCaptureVideoPreviewLayer).videoGravity = AVLayerVideoGravityResizeAspect;
}
}
希望有所帮助!