Skip to content

Commit b2aea2f

Browse files
committed
Supported camera view rotating, partially.
It is too difficult to support rotating perfectly.....
1 parent d5a6028 commit b2aea2f

5 files changed

+39
-26
lines changed

CameraViewController.h

+1
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ typedef enum {
7676
NSTimer *fpsTimer;
7777
int frameCounter;
7878
double fpsTimeStamp;
79+
BOOL canRotate;
7980
}
8081
- (id)initWithCameraViewControllerType:(CameraViewControllerType)value;
8182
- (void)startToMeasureFPS;

CameraViewController.m

+12-24
Original file line numberDiff line numberDiff line change
@@ -168,8 +168,8 @@ - (void)prepareWithCameraViewControllerType:(CameraViewControllerType)value {
168168
}
169169

170170
// setting preview layer
171-
previewView = [[UIView alloc] initWithFrame:CGRectZero];
172-
[previewView setAutoresizingMask:UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight];
171+
previewView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 1024, 1024)];
172+
[previewView setAutoresizingMask:UIViewAutoresizingNone];
173173

174174
previewLayer = [AVCaptureVideoPreviewLayer layerWithSession:session];
175175
}
@@ -184,36 +184,22 @@ - (id)initWithCameraViewControllerType:(CameraViewControllerType)value {
184184
}
185185

186186
- (void)adjustCameraPreviewLayerOrientaion:(UIInterfaceOrientation)orientation {
187-
CATransform3D m;
188-
CGRect f = previewLayer.frame;
189-
190-
float offsetx = (f.size.height - f.size.width)/2;
191-
float offsety = (f.size.width - f.size.height)/2;
192-
193187
switch(orientation) {
194188
case UIInterfaceOrientationLandscapeLeft:
195-
m = CATransform3DMakeTranslation(offsetx, offsety, 0);
196-
m = CATransform3DRotate(m, M_PI/2, 0, 0, 1);
189+
previewView.transform = CGAffineTransformMakeRotation(M_PI_2);
197190
break;
198191
case UIInterfaceOrientationLandscapeRight:
199-
m = CATransform3DMakeTranslation(offsetx, offsety, 0);
200-
m = CATransform3DRotate(m, 3 * M_PI/2, 0, 0, 1);
192+
previewView.transform = CGAffineTransformMakeRotation(M_PI_2);
201193
break;
202194
case UIInterfaceOrientationPortrait:
203-
m = CATransform3DMakeTranslation(0, -2, 0);
204-
m = CATransform3DRotate(m, 0, 0, 0, 1);
195+
previewView.transform = CGAffineTransformMakeRotation(0);
205196
break;
206197
case UIInterfaceOrientationPortraitUpsideDown:
207-
m = CATransform3DMakeTranslation(0, -2, 0);
208-
m = CATransform3DRotate(m, M_PI, 0, 0, 1);
198+
previewView.transform = CGAffineTransformMakeRotation(0);
209199
break;
210200
default:
211-
m = CATransform3DMakeTranslation(0, -2, 0);
212-
m = CATransform3DRotate(m, 0, 0, 0, 1);
213201
break;
214202
}
215-
216-
previewLayer.transform = m;
217203
}
218204

219205
- (void)waitForSessionStopRunning {
@@ -251,7 +237,7 @@ - (id)initWithCoder:(NSCoder *)coder {
251237

252238
- (void)viewWillAppear:(BOOL)animated {
253239
[super viewWillAppear:animated];
254-
240+
255241
float width = self.view.frame.size.width;
256242
float height = self.view.frame.size.width * aspectRatio;
257243

@@ -261,6 +247,8 @@ - (void)viewWillAppear:(BOOL)animated {
261247
[session startRunning];
262248

263249
[self adjustCameraPreviewLayerOrientaion:self.interfaceOrientation];
250+
251+
canRotate = YES;
264252
}
265253

266254
- (void)viewWillDisappear:(BOOL)animated {
@@ -276,9 +264,9 @@ - (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrie
276264
}
277265

278266
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
279-
return (interfaceOrientation == UIInterfaceOrientationPortrait);
280-
// for orientation test
281-
//return YES;
267+
// I can't understand the orienation behavior of view controllers.....
268+
// Recommend that you don't overide this method... or please help me.
269+
return canRotate;
282270
}
283271

284272
#pragma mark - AVCaptureVideoDataOutputSampleBufferDelegate

cv/NormalCameraViewController.m

+22-1
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,33 @@ @implementation NormalCameraViewController
3535

3636
- (void)viewWillAppear:(BOOL)animated {
3737
[super viewWillAppear:animated];
38+
39+
CGRect frame;
40+
switch(self.interfaceOrientation) {
41+
case UIInterfaceOrientationLandscapeLeft:
42+
frame = CGRectMake(0, self.view.frame.size.width - 44, self.view.frame.size.height, 44);
43+
break;
44+
case UIInterfaceOrientationLandscapeRight:
45+
frame = CGRectMake(0, self.view.frame.size.width - 44, self.view.frame.size.height, 44);
46+
break;
47+
case UIInterfaceOrientationPortrait:
48+
frame = CGRectMake(0, self.view.frame.size.height - 44, self.view.frame.size.width, 44);
49+
break;
50+
case UIInterfaceOrientationPortraitUpsideDown:
51+
frame = CGRectMake(0, self.view.frame.size.height - 44, self.view.frame.size.width, 44);
52+
break;
53+
default:
54+
frame = CGRectMake(0, self.view.frame.size.height - 44, self.view.frame.size.width, 44);
55+
break;
56+
}
3857

3958
// add toolbar
40-
UIToolbar *bar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, self.view.frame.size.height - 44, self.view.frame.size.width, 44)];
59+
UIToolbar *bar = [[UIToolbar alloc] initWithFrame:frame];//CGRectMake(0, self.view.frame.size.height - 44, self.view.frame.size.width, 44)];
60+
[bar setAutoresizingMask:UIViewAutoresizingFlexibleTopMargin|UIViewAutoresizingFlexibleWidth];
4161
[self.view addSubview:bar];
4262
[bar release];
4363

64+
// add button
4465
UIBarButtonItem *closeButton = [[[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"Close", nil) style:UIBarButtonItemStyleBordered target:self action:@selector(close:)] autorelease];
4566
[bar setItems:[NSArray arrayWithObject:closeButton]];
4667
}

cv/cvViewController.m

+4
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@
3636

3737
@implementation cvViewController
3838

39+
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
40+
return YES;
41+
}
42+
3943
- (IBAction)openCameraViewController:(id)sender {
4044
NormalCameraViewController *controller = [[NormalCameraViewController alloc] initWithCameraViewControllerType:BufferGrayColor|BufferSize640x480];
4145
[self presentModalViewController:controller animated:YES];

cv/en.lproj/cvViewController.xib

-1
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,6 @@
100100
<string key="NSFrame">{{73, 256}, {175, 37}}</string>
101101
<reference key="NSSuperview" ref="774585933"/>
102102
<reference key="NSWindow"/>
103-
<reference key="NSNextKeyView"/>
104103
<bool key="IBUIOpaque">NO</bool>
105104
<string key="targetRuntimeIdentifier">IBCocoaTouchFramework</string>
106105
<int key="IBUIContentHorizontalAlignment">0</int>

0 commit comments

Comments
 (0)