Layout issue when using AVCaptureVideoPreviewLayer - swift

I'm trying to learn AVFoundation basics. For now I'm trying to make the simplest custom camera demo possible. The general scheme is to use UIView as a container for AVCaptureVideoPreviewLayer
So I create a previewLayer in the following way:
let previewLayer = AVCaptureVideoPreviewLayer(session: captureSession)
As a following I do this:
previewLayer?.frame = self.imageView.bounds
self.imageView.layer.addSublayer(previewLayer)
captureSession.startRunning()
That's basically it. As a result the captureSession is being run but visually camera preview is misplaced inside the UIView. Please, see the picture below
However, I almost sure I did everything correctly in the storyboard:
I'm sorry I don't know what to add since I'm relatively new in swift. Please let me know if i miss any information required to answer.
So the question is what might be the reason for this kind of UI behaviour?
Thanks in advance.

You need to set view's content mode:
previewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;

First, assuming that you are using Autolayout, make sure your constraints are correct for your views in the storyboard. If you're not using Autolayout, I would recommend doing that first before proceeding.
You'll need to set the frame for your AVCaptureVideoPreviewLayer after adding it to the imageView.
previewLayer.frame = self.imageView.layer.bounds
I think that should take care of it. If you rotate your device, you may need to update the size of the previewLayer in your view controller's viewWillLayoutSubviews.

Related

Issues setting up a back ground image and with UIImageView

On my iPhone app, I simply want to set a particular background image, which depends on whether it's an iPhone 5 or not.
So, I tried two approaches:
A) Using
self.view.backgroundColor = [UIColor colorWithPatternImage:backGroundimage];
B) Creating an UIImageView and setting up the image there. Code:
UIImageView *backgroundImageView = [[UIImageView alloc]initWithFrame:screenBounds];
[backgroundImageView setImage:[UIImage imageNamed:backGroundImage]];
[self.view addSubview:backgroundImageView];
But I am having issues with both of them:
Issues with Step A:
When I set the image through that way, I have to deal with the image scaling issues for different sizes of the screen. I use the following code to do the scalling:
UIGraphicsBeginImageContext(screenBounds.size);
[[UIImage imageNamed:backGroundImage] drawInRect:screenBounds];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
self.view.backgroundColor = [UIColor colorWithPatternImage:image];
Another issue from Step A is that the image appears quite blurry. It doesn't have the same sharpness to it.
Issues with Step B:
With this, the image looks really crisp and sharp - just the way it should look.
But when I switch to another view using the following code, strangely enough the UIImageView backgroundImageView still appears on the second one. The code I use to switch views is:
[self presentViewController:secondViewController animated:YES completion:nil];
I even tried [backgroundImageView removeFromSuperview], but that doesn't solve anything either.
So what am I doing wrong? And how can I set up a picture as my background which is dependent on the size of the iphone?
Plan B is a good plan. Presenting another view controller should and will definitely hide your image view. If it isn't happening, then it's a problem with the creation of secondViewController, unrelated to the background image on the presenting VC.
Before presenting secondViewController, log it:
NSLog(#"presenting %#", secondViewController);
I'll bet a dollar that it's nil. If I'm right, let's have a look at how you initialize secondViewController. That's the problem, unrelated to the background image view.
Okay, I finally fixed this issue, although the cause of this issue is still puzzling to me.
To fix this, I had to create an IBOutlet property for UIImageView and hook it up on the XIB file.
The reason I was programmatically creating the UIImageView is because the size of the UIImageView depends on what size iPhone they are using. But for the IBOutlet (let's call it as UIImageViewOutlet, I simply used [self.UIImageViewOutlet setFrame:] to get the size and location that I wanted.
I also discovered that one of the buttons that I was programmatically creating, was still visible in the secondViewController. I ended up creating an Outlet on the XIB file for that one as well and used setFrame on it to position it properly.
If anyone who knows the reason of this problem, I will be very grateful.

iPhone:Photo booth count down feature in iPhone video recording feature?

I am doing video recording from my iPhone 4 application program. I want to do an enhancement same like photo booth's 3 .. 2 .. 1 count down prior to taking video recording. Is it possible to do that on my iPhone program programmatically? If YES, how and if NO why? Please advise.
Thank you in advance.
Yes.
All you really need to do is draw the 3,2 and 1 on the screen on top of your AVCapturePreviewLayer.
Here's is Apple's code from documentation:
AVCaptureSession *captureSession = <#Get a capture session#>;
AVCaptureVideoPreviewLayer *previewLayer = [AVCaptureVideoPreviewLayer layerWithSession:captureSession];
UIView *aView = <#The view in which to present the layer#>;
previewLayer.frame = aView.bounds; // Assume you want the preview layer to fill the view.
[aView.layer addSublayer:previewLayer];
so your part is simple:
[[aView superview] addSubview:countdownView];
Inside countdown view, you can have a custom draw method or just add UILabels. Lots of options on actually doing the countdown. You can use NSTimers to change the labels or even UIView animations with callbacks.

Show camera stream while AVCaptureSession's running

I was able to capture video frames from the camera using AVCaptureSession according to http://developer.apple.com/iphone/library/qa/qa2010/qa1702.html. However, it seems that AVCaptureScreen captures frames from the camera without showing the camera stream on the screen. I would like to also show camera stream just like in UIImagePicker so that the user knows that the camera is being turned on and sees what the camera is pointed at. Any help or pointer would be appreciated!
AVCaptureVideoPreviewLayer is exactly what you're looking for.
The code fragment Apple uses to demonstrate how to use it is:
AVCaptureSession *captureSession = <#Get a capture session#>;
AVCaptureVideoPreviewLayer *previewLayer = [AVCaptureVideoPreviewLayer layerWithSession:captureSession];
UIView *aView = <#The view in which to present the layer#>;
previewLayer.frame = aView.bounds; // Assume you want the preview layer to fill the view.
[aView.layer addSublayer:previewLayer];

Loading MPMoviePlayerViewController with transparent background?

i´ve added a MPMoviePlayerViewController instance and playing of a movie works great.
I´ve 3 buttons and want to load different videos in a UIView-container. That works, too.
But if i click on a button to load an other video, everytime the background is flickering black.
I´ve set the color to "clearColor":
player.moviePlayer.backgroundView.backgroundColor = [UIColor clearColor];
But that doesn´t help. Is there a way to load a video without a background - only the video-content?
Thanks for your time.
Not sure about the flickering issue. You said it flickers when you load another video -- Are you unintentionally layering multiple videos on top of each other? Make sure you remove the old one!
The black background likely is because your
MPMoviePlayerController's
scalingMode property is set to MPMovieScalingModeAspectFit (Apple's Documentation:
MPMoviePlayerController
scalingMode)
For issue #2, like you, I had expected setting the backgroundView's color to handle this, however it seems there is another view behind that which you also need to set the backgroundColor to clearColor. My hack for this was to simply iterate through the movie player's subviews and set their backgroundColor to clear.
Hack/"Solution" example using your variable name:
for(UIView* subV in player.moviePlayer.view.subviews) {
subV.backgroundColor = [UIColor clearColor];
}
You have to re-apply clearColor to the subviews any time you enter/exit fullscreen mode as well. I hope someone else has a better solution because this method seems pretty kludgy.
another option is to hide the video player and then show it when it is ready to display
caveat needs IO6>= i believe:
https://stackoverflow.com/a/19459855/401896

Preview rounded image in iphone

can anyone tell me how can I create an image preview inside a customized cell with the aspect of the ones loaded in the mms'.
I've been trying to do it by changing values in IB and I haven't been able to.
Thanks a lot!
(source: iphonehelp.in)
Thanks for the three answers.
Doing
cell3.imageView.layer.masksToBounds = YES;
cell3.imageView.layer.cornerRadius = 16;
cell3.imageView.layer.borderColor = [UIColor lightGrayColor].CGColor;
cell3.imageView.layer.borderWidth = 1.0;
I was able to do part of what I want. However, I still can't put that "glow" look on the image. Does anybody now which property should I control?
Thanks a lot (and thanks again).
Sure. Add the QuartzCore framework to your project, then:
#import <QuartzCore/QuartzCore.h>
// ...
cell.imageView.layer.masksToBounds = YES;
cell.imageView.layer.cornerRadius = 4;
Note that the cornerRadius property is only available in OS 3.0 and later; if you're supporting an older version (which you probably don't have to be), you'll need to do some Core Graphics work.
will creating an image in an image editor and then setting it as the cell's background not work?
This might be useful for you
http://cocoawithlove.com/2009/04/easy-custom-uitableview-drawing.html
If your image is in an image view and you're targeting OS 3.0+, you can set imageView.layer.cornerRadius.