最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

swift - Show cameraView from subView - Stack Overflow

programmeradmin4浏览0评论

This question is about displaying a camera view in a subview as opposed to the main self.view.

According to AI, it should be possible to display it in a subview as long as the subview is in the view hierarchy, but I can't make this work. I'm wondering if there is somethign special you need to do with respect to the Z index of the subview, its alpha value or something else.

Is it true as various AIs say that you can display a cameraView through a subView as well as through the main view? If so what other steps beyond assigning the subView a place in the hierarchy are needed?

Code:

View Controller (in Obj-c)

    - (void)viewDidLoad {
        [super viewDidLoad];
        UIView *aView = [UIView new];
        [self.view addSubview: aView];//have tried this and following
        //[self.view insertSubview:aView atIndex:0];
        //[self.view bringSubviewToFront:aView];//also tried this
        
        aView.translatesAutoresizingMaskIntoConstraints = false;
       float height = self.previewView.frame.size.height;
        float width = self.previewView.frame.size.width;
        aView.alpha = 0.1;
        [aView.widthAnchor constraintEqualToConstant:width].active = YES;
        [aView.heightAnchor constraintEqualToConstant:height].active = YES;
        [aView.centerXAnchor constraintEqualToAnchor:self.view.centerXAnchor].active = YES;
        [aView.topAnchor constraintEqualToAnchor:self.view.topAnchor constant:25].active = YES;
    //  [[Utilities shared] showLivePreviewWithView:self.view];//works
        [[Utilities shared] showLivePreviewWithView:aView];//does not work
}



   

This question is about displaying a camera view in a subview as opposed to the main self.view.

According to AI, it should be possible to display it in a subview as long as the subview is in the view hierarchy, but I can't make this work. I'm wondering if there is somethign special you need to do with respect to the Z index of the subview, its alpha value or something else.

Is it true as various AIs say that you can display a cameraView through a subView as well as through the main view? If so what other steps beyond assigning the subView a place in the hierarchy are needed?

Code:

View Controller (in Obj-c)

    - (void)viewDidLoad {
        [super viewDidLoad];
        UIView *aView = [UIView new];
        [self.view addSubview: aView];//have tried this and following
        //[self.view insertSubview:aView atIndex:0];
        //[self.view bringSubviewToFront:aView];//also tried this
        
        aView.translatesAutoresizingMaskIntoConstraints = false;
       float height = self.previewView.frame.size.height;
        float width = self.previewView.frame.size.width;
        aView.alpha = 0.1;
        [aView.widthAnchor constraintEqualToConstant:width].active = YES;
        [aView.heightAnchor constraintEqualToConstant:height].active = YES;
        [aView.centerXAnchor constraintEqualToAnchor:self.view.centerXAnchor].active = YES;
        [aView.topAnchor constraintEqualToAnchor:self.view.topAnchor constant:25].active = YES;
    //  [[Utilities shared] showLivePreviewWithView:self.view];//works
        [[Utilities shared] showLivePreviewWithView:aView];//does not work
}



   
Share edited Mar 25 at 22:58 halfer 20.4k19 gold badges109 silver badges202 bronze badges asked Mar 9 at 0:29 user6631314user6631314 1,9801 gold badge18 silver badges60 bronze badges 6
  • This question is similar to: Camera preview not visible in view controller. If you believe it’s different, please edit the question, make it clear how it’s different and/or how the answers on that question are not helpful for your problem. – HangarRash Commented Mar 9 at 0:40
  • The questions are completely different. Please re read them: The first question was how to get the camera to display immediately without having to go to the camera and reverse its direction. The second question is how (assuming the code works) to display the camera in a view other than main self.view, in other words a subview in the hierarcy. Two totally different topics. Please reread and this should be immediately apparent.I have removed the shared code which may have confused you. – user6631314 Commented Mar 9 at 2:26
  • But why two separate questions for the same basic issue? There should only be one question with both sets of code. One question asking why your camera view doesn't display correctly with the given code. This question can't be answered without seeing what you do in the showLivePreview function which you show in the other question. You'll get much better help having one complete question instead of two partial but related questions. – HangarRash Commented Mar 9 at 4:11
  • Your bio says you are an iOS developer. Perhaps you can answer this question. Is it possible to show a live camera preview through a subview as opposed to self.view? Yes or no? That would be really helpful... – user6631314 Commented Mar 9 at 17:17
  • I can't see any reason why it can't be shown in some subview. – HangarRash Commented Mar 9 at 17:24
 |  Show 1 more comment

1 Answer 1

Reset to default 0

It is possible to display a cameraview in a subview as opposed to the main view providing it is part of the view hierarchy. However, I found it necessary place it in the view hierarchy earlier in the process so that when you assign the camera layer to the the view the views have been laid out so that they display properly.

Rather than trying to create the view programmatically right before attaching the camera view as in the above code when the views were not yet laid out, I was able to get it to work by creating the view In Storyboard, giving it constraints and then giving it an IBOutlet to an instance variable. Then when it comes time to show the camera preview, assign the camera layer to the instance variable.

In short what worked for me was:

Create UIView in Storyboard and give it constraints.

Create an outlet from the UIView to an instance variable in the View Controller.

Assign the camera layer to the instance variable of the UIView.

发布评论

评论列表(0)

  1. 暂无评论