2013-12-15 5 views
9

Ich weiß, dass dies eine Reihe von Zeit gefragt wurde, aber ich kann keine Antwort finden.Verwendung von cameraOverlayView mit UIImagePickerController

Ich habe eine App, die UIImagePickerController verwendet, um ein Bild zu machen. Das Problem ist, dass ich nur die Kamera-Option verfügbar sein soll, und ich verstehe, dass ich die Standard-Kontrollen zu verstecken brauchen:

cameraUI.showsCameraControls=NO; 

und verwenden Sie einen cameraOverlayView meine eigenen Kontrollen zur Verfügung zu stellen. Ich habe mir schon das Projekt PhotoPicker von Apple angesehen und mein erstes Problem ist, wie bekomme ich ein Overlay Objekt auf mein Storyboard? Ich kann ein solches Objekt in der Bibliothek nicht finden. Jede Hilfe wurde dankbar angenommen. Hier

Antwort

14

ist der Code:

toolBar=[[UIToolbar alloc] initWithFrame:CGRectMake(0, self.view.frame.size.height-54, self.view.frame.size.width, 55)]; 

toolBar.barStyle = UIBarStyleBlackOpaque; 
NSArray *items=[NSArray arrayWithObjects: 
       [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(cancelPicture)], 
       [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil], 
       [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCamera target:self action:@selector(shootPicture)], 
       [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil], 
       [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil], 
       nil]; 
[toolBar setItems:items]; 

// create the overlay view 
overlayView = [[OverlayView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height-44)]; 

// important - it needs to be transparent so the camera preview shows through! 
overlayView.opaque=NO; 
overlayView.backgroundColor=[UIColor clearColor]; 

// parent view for our overlay 
UIView *cameraView=[[UIView alloc] initWithFrame:self.view.bounds]; 
[cameraView addSubview:overlayView]; 
[cameraView addSubview:toolBar]; 

imagePickerController = [[UIImagePickerController alloc] init]; 

if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera] == NO){ 
    NSLog(@"Camera not available"); 
    return; 
} 
imagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera; 
imagePickerController.delegate = self; 

// hide the camera controls 
imagePickerController.showsCameraControls=NO; 
imagePickerController.wantsFullScreenLayout = YES; 
[imagePickerController setCameraOverlayView:cameraView]; 

[self presentViewController:imagePickerController animated:YES completion:nil]; 

Deklarieren Sie diese in Ihrer Header-Datei:

UIImagePickerController * imagePickerController; 
UIToolbar *toolBar; 
OverlayView *overlayView; 

Diese OverlayView.h und .m Klasse von Apples PhotoPicker.

Aktionen für Foto Aufnahme benutzerdefinierte Kamerataste:

-(void) shootPicture { 
    [imagePickerController takePicture]; 
} 

- (IBAction)cancelPicture { 
    [self dismissViewControllerAnimated:YES completion:nil]; 
} 

Ausgabe wie folgt unten angebracht Screenshot kommen (ich habe eine Aufnahmetaste und Abbruchtaste in benutzerdefinierten Overlay-Ansicht hinzugefügt):

enter image description here

Happy Coding :)

+1

Können Sie einige weitere Details geben, wo diese Klasse Overlay herkommt? –

+0

Ich denke, die OverlayView-Klasse ist nur eine andere UIView? – cdub

1

Hier ist mein Code. Wenn Sie möchten, dass die Kamera angezeigt wird, sobald der View-Controller geöffnet wird, stellen Sie sicher, dass Sie den UIImagePickerController in viewDidAppear wie ich initialisiert haben (viewDidLoad funktioniert nicht).

@interface CameraViewController() 
@property UIImagePickerController *PickerController; 
@property CGFloat HeightOfButtons; 
@end 


- (UIView *)createCustomOverlayView 
{ 

    // Main overlay view created 
    UIView *main_overlay_view = [[UIView alloc] initWithFrame:self.view.bounds]; 

    // Clear view (live camera feed) created and added to main overlay view 
    // ------------------------------------------------------------------------ 
    UIView *clear_view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - self.HeightOfButtons)]; 
    clear_view.opaque = NO; 
    clear_view.backgroundColor = [UIColor clearColor]; 
    [main_overlay_view addSubview:clear_view]; 
    // ------------------------------------------------------------------------ 


    // Creates two red buttons on the bottom of the view (on top of the live camera feed) 
    // Then adds the buttons to the main overlay view 
    // You can, of course, customize these buttons however you want 
    // ------------------------------------------------------------------------ 
    for(int i = 0; i < 2; i++) { 
     self.HeightOfButtons = 100; 
     UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; 
     // when a button is touched, UIImagePickerController snaps a picture 
     [button addTarget:self action:@selector(testIfButtonResponds) forControlEvents:UIControlEventTouchUpInside]; 
     button.frame = CGRectMake(i * self.view.frame.size.width/2, self.view.frame.size.height - self.HeightOfButtons, self.view.frame.size.width/2, self.HeightOfButtons); 
     [button setBackgroundColor:[UIColor redColor]]; 
     [main_overlay_view addSubview:button]; 
    } 
    // ------------------------------------------------------------------------ 

    return main_overlay_view; 
} 

- (void)makeCustomCameraAppear 
{ 
    self.PickerController = [[UIImagePickerController alloc] init]; 
    self.PickerController.sourceType = UIImagePickerControllerSourceTypeCamera; 
    self.PickerController.showsCameraControls = NO; 
    self.PickerController.delegate = self; 

    UIView *overlay_view = [self createCustomOverlayView]; 
    [self.PickerController setCameraOverlayView:overlay_view]; 

    [self presentViewController:self.PickerController animated:YES completion:NULL]; 
} 

- (void)viewDidAppear:(BOOL)animated 
{ 
    [self makeCustomCameraAppear]; 
} 
0

große Informationen. Um ein wenig zu dem, was Raghu tat:

up top:

UIImagePicker *_imagePicker; 

//inside your take a picture method 
UIToolbar *toolBar=[[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 100)]; 
toolBar.barStyle = UIBarStyleBlackOpaque; 
NSArray *items=[NSArray arrayWithObjects: 
       [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(cancelPicker)], 
       [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil], 
       [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCamera target:self action:@selector(shootPicture)], 
       [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil], 
       [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil], 
       nil]; 
[toolBar setItems:items]; 
toolBar.backgroundColor=[UIColor blackColor]; 



UIImageView *tp=[[UIImageView alloc]initWithFrame:CGRectMake(0, self.view.frame.size.height-100, self.view.frame.size.width, 100)]; 

tp.userInteractionEnabled=YES; 
tp.backgroundColor=[UIColor blackColor]; 

[tp addSubview:toolBar]; //add the toolbar to the uiimageview 

_imagePicker=[[UIImagePickerController alloc]init]; 
_imagePicker.sourceType=UIImagePickerControllerSourceTypeCamera; 
_imagePicker.delegate = self; 
_imagePicker.showsCameraControls=NO; //we will make custom controls. 
[_imagePicker.view addSubview:tp]; 



-(void)cancelPicker{ 
//get rid of the image picker 
[self dismissViewControllerAnimated:YES completion:nil]; //dismiss uiimagepicker 
_imagePicker=nil; 
} 

//take the picture, it will then go directly to - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info method 
-(void)shootPicture{ 
[_imagePicker takePicture]; 
}