简体   繁体   English

在ViewController中实例化UIImageView时如何设置其大小/框架和contentMode?

[英]How to set size/frame and contentMode of a UIImageView when its instantiated in the viewController?

I have a view controller for my app that instatiates previewView, capturedImage and CapturedImageView like so 我的应用程序有一个视图控制器,它可以像这样静态化PreviewView,capturedImage和CapturedImageView

class ViewController: UIViewController, AVCaptureFileOutputRecordingDelegate, UIGestureRecognizerDelegate {
    @IBOutlet weak var previewView: AVCamPreviewView!

    var capturedImage: UIImage!
    var capturedImageView: UIImageView!
}

previewView is on the main.storyboard but the capturedImage and CapturedImageView are not. 预览视图位于main.storyboard上,但capturedImage和CapturedImageView不在。 I want capturedImageView to be the exact same size as the previewView so normally I would do something like let capturedImageView = UIImageView(frame: self.previewView.frame) 我希望capturedImageView的大小与previewView的大小完全相同,因此通常我会做类似let capturedImageView = UIImageView(frame: self.previewView.frame)

but I don't seem to be able to use that syntax outside of a function. 但我似乎无法在函数外部使用该语法。 Additionally I'm not sure if the previewView actually exists yet for me to be able to do this.... 此外,我不确定预览视图是否确实存在,但我无法执行此操作。

It complains Value of type 'NSObject -> () -> ViewController' has no member 'previewView' 它抱怨Value of type 'NSObject -> () -> ViewController' has no member 'previewView'

so I think it doesn't 所以我认为不是

so maybe I could do it after in viewDidLoad? 所以也许我可以在viewDidLoad之后做呢? But again I don't think I can just use self.capturedImageView = UIImageView(frame: self.previewView.frame) seeing as when I tried it I got this long hairy error message (and the app froze): 但是再次,我认为我不能只使用self.capturedImageView = UIImageView(frame: self.previewView.frame)看到我尝试此操作时收到了这条长毛错误消息(并且该应用程序冻结了):

function signature specialization <Arg[0] = Exploded, Arg[1] = Exploded> of Swift.(_fatalErrorMessage (Swift.StaticString, Swift.StaticString, Swift.StaticString, Swift.UInt) -> ()).(closure #2)

Any suggestions? 有什么建议么?

I'm not sure how clear it is what you are trying to do — in the code you have given, you haven't instantiated anything. 我不确定您要执行的操作有多清晰-在您提供的代码中,您尚未实例化任何内容。

If you are trying to instantiate capturedImageView directly with its declaration in the view controller, and referencing another property in the same view controller, you will not be able to do that, since you can only reference self (ie the view controller) once it is fully instantiated, and all its properties must be fully initialised before that. 如果您试图直接在视图控制器中使用其声明直接实例化capturedImageView ,并在同一视图控制器中引用另一个属性,则将无法执行此操作,因为一旦引用了self (即视图控制器),便只能对其进行引用。完全实例化,并且其所有属性必须在此之前完全初始化。

Therefore you should either instantiate capturedImageView without a frame (and therefore not referencing self ), and then set its frame later on (in viewDidLoad for example, or you could make capturedImageView into a lazy property (which can then reference self since it will only be instantiated after full instantiation of the view controller. 因此,您应该实例化不带框架的capturedImageView (因此不引用self ),然后稍后再设置其框架(例如在viewDidLoad中),或者可以将capturedImageView为惰性属性(该属性可以引用self因为它只会被引用)在完全实例化视图控制器之后实例化。

If you are making frames of views equal to one another, you would of course be better off using autolayout, but that is a separate issue. 如果要使视图框架彼此相等,那么使用自动布局当然会更好,但这是一个单独的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM