Knowledge Base Nr: 00344 cameraoverlay.m - http://www.swe-kaiser.de
Downloads:
ObjC: Camera Overlay
- (UIView*)createCameraOverlay:(UIImagePickerController*)cameraUI
{
CGRect frameDlg = IS_IPADMODE ? CGRectMake(0, 0, 1024, 768) : CGRectMake(0, 0, 700, 700);
UIView* overlay = [[UIView alloc] initWithFrame:frameDlg];
//overlay.alpha = 0.5f;
if (IS_IPADMODE)
co_imageBackground = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"screen_warte.jpg"]];
else
co_imageBackground = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"background_iphone.png"]];
co_imageBackground.frame = IS_IPADMODE ? CGRectMake(0, 0, 1024, 768) : CGRectMake(0, 0, 1024, 768);
[overlay addSubview:co_imageBackground];
UIImage *imageOk;
if (IS_IPADMODE)
imageOk = [UIImage imageNamed:@"picture_use.png"];
else
imageOk = [UIImage imageNamed:@"take_picture_iphone.png"];
co_buttonOK = [UIButton buttonWithType:UIButtonTypeRoundedRect];
co_buttonOK.frame = IS_IPADMODE ? CGRectMake(954.0, 84.0, 58, 600) : CGRectMake(10.0, 450.0, 300, 30);
[co_buttonOK setBackgroundImage:imageOk forState:UIControlStateNormal];
[co_buttonOK addTarget:cameraUI action:@selector(takePicture) forControlEvents:UIControlEventTouchUpInside];
[overlay addSubview:co_buttonOK];
UIImage *imageCancel;
if (IS_IPADMODE)
imageCancel = [UIImage imageNamed:@"picture_close.png"];
else
imageCancel = [UIImage imageNamed:@"discard_picture_iphone.png"];
co_buttonCancel = [UIButton buttonWithType:UIButtonTypeRoundedRect];
co_buttonCancel.frame = IS_IPADMODE ? CGRectMake(10.0, 84.0, 58, 600) : CGRectMake(10.0, 0.0, 300, 30);
[co_buttonCancel setBackgroundImage:imageCancel forState:UIControlStateNormal];
[co_buttonCancel addTarget:self action:@selector(cancelPhoto) forControlEvents:UIControlEventTouchUpInside];
[overlay addSubview:co_buttonCancel];
UIImage *imageTextOk = [UIImage imageNamed:@"picture_save.png"];
co_buttonTextOK = [UIButton buttonWithType:UIButtonTypeRoundedRect];
co_buttonTextOK.frame = IS_IPADMODE ? CGRectMake(790, 310, 230, 47) : CGRectMake(170, 230, 150, 50);
[co_buttonTextOK setBackgroundImage:imageTextOk forState:UIControlStateNormal];
[co_buttonTextOK addTarget:self action:@selector(madePictureTextOk) forControlEvents:UIControlEventTouchUpInside];
[overlay addSubview:co_buttonTextOK];
UIImage *imageTextCancel = [UIImage imageNamed:@"picture_cancel.png"];
co_buttonTextCancel = [UIButton buttonWithType:UIButtonTypeRoundedRect];
co_buttonTextCancel.frame = IS_IPADMODE ? CGRectMake(20, 310, 230, 47) : CGRectMake(0, 230, 150, 50);
[co_buttonTextCancel setBackgroundImage:imageTextCancel forState:UIControlStateNormal];
[co_buttonTextCancel addTarget:self action:@selector(madePictureTextCancel) forControlEvents:UIControlEventTouchUpInside];
[overlay addSubview:co_buttonTextCancel];
co_imageTextBackground = [[UIImageView alloc] initWithImage:[UIImage imageNamed:IS_IPADMODE ? @"bemerkung-zum-foto.jpg" : @"notice-foto-iphone.png"]];
co_imageTextBackground.frame = IS_IPADMODE ? CGRectMake(100.0, 40.0, 825, 110) : CGRectMake(10.0, 50.0, 300, 170);
[overlay addSubview:co_imageTextBackground];
co_labelWarten = [[UILabel alloc] initWithFrame: IS_IPADMODE ? CGRectMake(150, 150.0, 1024-300, 60) : CGRectMake(13, 33, 280, 30)];
[co_labelWarten setText:@"Bild wird gespeichert..."];
co_labelWarten.textAlignment = NSTextAlignmentCenter;
co_labelWarten.textColor = [UIColor whiteColor];
co_labelWarten.backgroundColor = [UIColor clearColor];
co_labelWarten.font = [UIFont fontWithName:@"Arial Rounded MT Bold" size:(24.0)];
[overlay addSubview:co_labelWarten];
co_textComment = [[UITextField alloc] initWithFrame: IS_IPADMODE ? CGRectMake(105, 80.0, 810, 60) : CGRectMake(13, 73, 292, 146)];
[co_textComment setBackgroundColor:[UIColor whiteColor]];
[co_textComment setFont:[UIFont fontWithName:@"Helvetica" size:24.0]];
[co_textComment setText:@""];
[overlay addSubview:co_textComment];
co_waitInd = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
co_waitInd.center = IS_IPADMODE ? CGPointMake(512, 234) : CGPointMake(160, 250);
[overlay addSubview:co_waitInd];
return overlay;
}
- (void) switchCameraOverlay:(ZPCamStateType)eState
{
NSString* sComment = [co_textComment text];
int len = [sComment length];
int idx = len;
for (; idx>0 ; idx--)
{
NSString* c = [sComment substringWithRange:NSMakeRange(idx-1, 1)];
if ( ([c compare:@"0"] != NSOrderedSame)
&& ([c compare:@"1"] != NSOrderedSame)
&& ([c compare:@"2"] != NSOrderedSame)
&& ([c compare:@"3"] != NSOrderedSame)
&& ([c compare:@"4"] != NSOrderedSame)
&& ([c compare:@"5"] != NSOrderedSame)
&& ([c compare:@"6"] != NSOrderedSame)
&& ([c compare:@"7"] != NSOrderedSame)
&& ([c compare:@"8"] != NSOrderedSame)
&& ([c compare:@"9"] != NSOrderedSame)
)
{
break;
}
}
BOOL bHasNumberAtEnd = (idx != len);
if (bHasNumberAtEnd)
{
NSString* sNumber = [sComment substringWithRange:NSMakeRange(idx, len-idx)];
int nNumber = [sNumber intValue] + 1;
sComment = [[NSString alloc] initWithFormat:@"%@%d", [sComment substringToIndex:idx], nNumber];
}
switch (eState)
{
case ZPCamLive:
[co_buttonOK setHidden:NO];
[co_buttonCancel setHidden:NO];
[co_buttonTextOK setHidden:YES];
[co_buttonTextCancel setHidden:YES];
[co_imageTextBackground setHidden:YES];
[co_textComment setHidden:YES];
[co_imageBackground setHidden:YES];
[co_waitInd stopAnimating];
[co_waitInd setHidden:YES];
[co_labelWarten setHidden:YES];
break;
case ZPCamAsk:
[co_buttonOK setHidden:YES];
[co_buttonCancel setHidden:YES];
[co_buttonTextOK setHidden:NO];
[co_buttonTextCancel setHidden:NO];
[co_imageTextBackground setHidden:NO];
[co_textComment setText:sComment];
[co_textComment setHidden:NO];
[co_imageBackground setHidden:NO];
[co_waitInd stopAnimating];
[co_waitInd setHidden:YES];
[co_labelWarten setHidden:YES];
break;
case ZPCamSaving:
[co_buttonOK setHidden:YES];
[co_buttonCancel setHidden:YES];
[co_buttonTextOK setHidden:YES];
[co_buttonTextCancel setHidden:YES];
[co_imageTextBackground setHidden:YES];
[co_textComment setHidden:YES];
[co_imageBackground setHidden:NO];
[co_waitInd startAnimating];
[co_waitInd setHidden:NO];
[co_labelWarten setHidden:NO];
break;
default:
ZPASSERT(@"switchCameraOverlay: unknown eState = %d", eState);
break;
}
}
- (BOOL) startCameraControllerFromViewController: (UIViewController*) controller
usingDelegate: (id <UIImagePickerControllerDelegate,
UINavigationControllerDelegate>) delegate
{
if (m_bCameraRunning)
return TRUE;
if (([UIImagePickerController isSourceTypeAvailable:
UIImagePickerControllerSourceTypeCamera] == NO)
|| (delegate == nil)
|| (controller == nil))
return NO;
UIImagePickerController *cameraUI = [[UIImagePickerController alloc] init];
cameraUI.sourceType = UIImagePickerControllerSourceTypeCamera;
// Displays a control that allows the user to choose picture or
// movie capture, if both are available:
cameraUI.mediaTypes = [[NSArray alloc] initWithObjects: (NSString *) kUTTypeImage, nil];
// Hides the controls for moving & scaling pictures, or for
// trimming movies. To instead show the controls, use YES.
cameraUI.allowsEditing = USERCANEDITIMAGE;
cameraUI.delegate =self;
cameraUI.showsCameraControls = NO;
/*
UIToolbar *theToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(850, 336, 90, 32)];
UIBarButtonItem *cameraBarButtonItemOK = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemCamera target:cameraUI action:@selector(takePicture)];
UIBarButtonItem *cameraBarButtonItemCancel = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonItemStyleBordered target:self action:@selector(cancelPhoto)];
theToolbar.items = [NSArray arrayWithObjects: cameraBarButtonItemOK, cameraBarButtonItemCancel, nil];
[cameraUI setCameraOverlayView:theToolbar];
*/
UIView* overlay = [self createCameraOverlay:cameraUI];
[cameraUI setCameraOverlayView:overlay];
[self switchCameraOverlay:ZPCamLive];
// Prevent the image picker learning about orientation changes by preventing the device from reporting them.
UIDevice *currentDevice = [UIDevice currentDevice];
// The device keeps count of orientation requests. If the count is more than one, it continues detecting them and sending notifications. So, switch them off repeatedly until the count reaches zero and they are genuinely off.
// If orientation notifications are on when the view is presented, it may slide on in landscape mode even if the app is entirely portrait.
// If other parts of the app require orientation notifications, the number "end" messages sent should be counted. An equal number of "begin" messages should be sent after the image picker ends.
m_nNotificationCount = 0;
while ([currentDevice isGeneratingDeviceOrientationNotifications])
{
[currentDevice endGeneratingDeviceOrientationNotifications];
m_nNotificationCount++;
}
// Display the camera.
[controller presentViewController:cameraUI animated:NO completion:nil];
// The UIImagePickerController switches on notifications AGAIN when it is presented, so switch them off again.
while ([currentDevice isGeneratingDeviceOrientationNotifications])
{
[currentDevice endGeneratingDeviceOrientationNotifications];
m_nNotificationCount++;
}
m_bCameraRunning = true;
return YES;
}