One-tap UIImagePickerController (iPhone camera)

来源:互联网 发布:ni软件是什么 编辑:程序博客网 时间:2024/05/21 09:51

I’ve been working on a number of iPhone camera applications. The default photograph functionality seemed kind of awkward to me, and I wanted to have a much simpler process and interface. This is an example of an Xcode project which shows how to load the camera interface with a custom overlaid image, and then wait for a tap anywhere on the screen to take a photo.

To start, I created a new project (a View-based application) called CameraTapper.

I knew I would need to subclass UIImagePickerController, but there’s really not much I needed to over-ride. I created a new file (with UIView as a base) called CustomCamera.m (and .h) and then modified them to support the “tap anywhere to capture” functionality:

CustomCamera.h

?
1
2
3
4
5
6
7
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
  
@interface CustomCamera : UIImagePickerController {   
}
  
@end

CustomCamera.m

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#import "CustomCamera.h"
  
@implementation CustomCamera
  
-(void) dealloc {
    [super dealloc];
}
  
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    // override the touches ended method
        // so tapping the screen will take a picture
    [self takePicture];
}
  
- (void)viewDidDisappear:(BOOL)animated {
    [super viewDidDisappear:animated];
}
  
-(void) viewDidAppear: (BOOL)animated {
    [super viewDidAppear:animated];   
}
  
@end

I also added an overlay to the project (a 24 bit transparent png):

camera view overlay

This overlay would give the user the visual clue of what to do in order to progress to the next step.

The rest of the code is simply there to demonstrate how this could be implemented in another project. I made a simple button within the CameraTapperViewController.m file to load the CustomCamera view, and I added functionality to show how the “taken” photograph could be used after it had been taken — by adding it to the background of the main view itself.

CameraTapperViewController.h

?
1
2
3
4
5
6
7
8
9
#import <UIKit/UIKit.h<
  
@interface CameraTapperViewController : UIViewController
<UIImagePickerControllerDelegate, UINavigationControllerDelegate> {
    UIImageView *myImageView;
    UIImage *myImage;
}
  
@end

CameraTapperViewController.m

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#import "CameraTapperViewController.h"
#import "CustomCamera.h"
  
@implementation CameraTapperViewController
  
- (void) launchCamera {
     
    // Set up the camera
    CustomCamera *cameraController = [[CustomCamera alloc] init];
    cameraController.sourceType =
          UIImagePickerControllerSourceTypeCamera;
    cameraController.delegate = self;
     
    cameraController.showsCameraControls = NO;
    cameraController.navigationBarHidden = YES;
    cameraController.toolbarHidden = YES;
     
    // overlay on top of camera lens view
    UIImageView *cameraOverlayView = [[UIImageView alloc]
          initWithImage:[UIImage imageNamed:@"camera_overlay.png"]];
    cameraOverlayView.alpha = 0.0f;
    cameraController.cameraOverlayView = cameraOverlayView;
     
    // animate the fade in after the shutter opens
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDelay:2.2f];
    cameraOverlayView.alpha = 1.0f;
    [UIView commitAnimations];
     
    [cameraOverlayView release];
     
    [self presentModalViewController:cameraController animated:YES];
}
  
// User took an image
- (void)imagePickerController:(UIImagePickerController *)picker
  didFinishPickingImage:(UIImage *)inImage
  editingInfo:(NSDictionary *)editingInfo {
    myImage = inImage;
    myImageView.image = myImage;
     
    // Get rid of the picker interface
    [[picker parentViewController]
          dismissModalViewControllerAnimated:YES];
    [picker release];   
}
  
- (void)viewDidLoad {
     
        // display the background image view
    myImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
    [self.view addSubview:myImageView];
     
        // display the button to launch the camera
    UIButton *btnLaunchCamera = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    btnLaunchCamera.frame = CGRectMake(60, 220, 200, 40);
    [btnLaunchCamera setTitle:@"Launch Camera" forState:UIControlStateNormal];
    [btnLaunchCamera addTarget:self action:@selector(launchCamera) forControlEvents:UIControlEventTouchUpInside];
     
    [self.view addSubview:btnLaunchCamera];
     
    [super viewDidLoad];
}
  
  
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}
  
- (void)viewDidUnload {
}
  
- (void)dealloc {
    [myImageView release];
    [super dealloc];
}
  
@end

For further exploration, I’d like to start playing with taking a real-time (or near real-time) feed of images and manipulating them — similar to some of the augmented reality applications that are currently being developed…

The full XCode project is linked here: CameraTapper

0 0
原创粉丝点击