1天学习1个类 UIImagePickerController 示例

真机就不截图了.

直接发代码,大家可以拷贝代码直接测试~

//
//  main.m
//  ControlDemo
//
//  Created by watsy0007 on 12-6-3.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController<UINavigationControllerDelegate,UIImagePickerControllerDelegate> {
}

@end


@implementation ViewController

- (void) dealloc {
    
    [super dealloc];
}

- (void) RecordMovie:(id) sender {
   
    @autoreleasepool {
         UIImagePickerController *imagePickerController = (UIImagePickerController *) sender;
        
        sleep(2);
        
        if (imagePickerController.cameraCaptureMode == UIImagePickerControllerCameraCaptureModePhoto) {
            [imagePickerController takePicture];
        } else {
            [imagePickerController startVideoCapture];
            sleep(10);
            [imagePickerController stopVideoCapture];
        }  
    }
    
}

- (void) BarItemAction:(id) sender {
    
    
    if ([UIImagePickerController availableMediaTypesForSourceType:UIImagePickerControllerSourceTypeCamera]) {
        UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
        imagePickerController.delegate = self;
        imagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera;
        //允许编辑
        imagePickerController.allowsEditing = YES; 
//        //相机模式
//        imagePickerController.cameraCaptureMode = UIImagePickerControllerCameraCaptureModeVideo;
        //前后
        imagePickerController.cameraDevice = UIImagePickerControllerCameraDeviceRear;
        //闪光灯模式
        imagePickerController.cameraFlashMode = UIImagePickerControllerCameraFlashModeOn;
        
        //叠加view
        UIView *view = [[UIView alloc] initWithFrame:CGRectMake(100, 100, 50, 50)];
        view.center = self.view.center;
        view.backgroundColor = [UIColor lightTextColor];
        imagePickerController.cameraOverlayView = view;
        [view release];
        
        //旋转视图
        imagePickerController.cameraViewTransform = CGAffineTransformMakeScale(0.5, 0.5);
        
        //允许模式
        imagePickerController.mediaTypes =
        [UIImagePickerController availableMediaTypesForSourceType:
         UIImagePickerControllerSourceTypeCamera];
        
        //最长时间支持
        imagePickerController.videoMaximumDuration = 100;
        
        //视频质量
        imagePickerController.videoQuality = UIImagePickerControllerQualityTypeLow;
        
        NSLog(@"\n[UIImagePickerController availableCaptureModesForCameraDevice]\n{\n\t%@\n}",[UIImagePickerController availableCaptureModesForCameraDevice:UIImagePickerControllerCameraDeviceFront]);
        
        NSLog(@"\n[UIImagePickerController isCameraDeviceAvailable]\n{\n\t%d\n}",[UIImagePickerController isCameraDeviceAvailable:UIImagePickerControllerCameraDeviceFront]);
        
        NSLog(@"\n[UIImagePickerController isFlashAvailableForCameraDevice]\n{\n\t%d\n}",[UIImagePickerController isCameraDeviceAvailable:UIImagePickerControllerCameraDeviceFront]);
        
         NSLog(@"\n[UIImagePickerController isSourceTypeAvailable]\n{\n\t%d\n}",[UIImagePickerController isCameraDeviceAvailable:UIImagePickerControllerCameraDeviceFront]);
        
//        imagePickerController.showsCameraControls = NO;
        [self presentModalViewController:imagePickerController animated:YES];
        
        [NSThread detachNewThreadSelector:@selector(RecordMovie:) 
                                 toTarget:self 
                               withObject:imagePickerController];
        
        [imagePickerController release];
        
    } else {
        for (id label in self.view.subviews) {
            if ([label isKindOfClass:[UILabel class]]) {
                [(UILabel*)label setText:@"不支持模拟器"];
            }
        }
    }
    
    
}

- (UIBarButtonItem *) createNewItem {
    UIBarButtonItem *item;
    
    item = [[UIBarButtonItem alloc] initWithTitle:@"相机" style:UIBarButtonItemStylePlain target:self 
                                           action:@selector(BarItemAction:)];
    
    return [item autorelease];
}

- (void) loadView {
    [super loadView];
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectZero];
    label.frame = CGRectMake(0, 0, 320, 420);
    label.numberOfLines = 3;
    label.textAlignment = UITextAlignmentCenter;
    label.text = @"watsy0007 \n QQ:258841679\n群:125807534";
    [self.view addSubview:label];
    [label release];
    
    self.title = @"UIDatePicker Class Reference Demo";
    
    self.navigationItem.rightBarButtonItem = [self createNewItem];
}


- (BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation {
    return YES;
}

- (void) viewDidLoad {
    [super viewDidLoad];
}

#pragma mark -
#pragma mark UIImagePickerDelegate delegate
- (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo {
    NSLog(@"\ndidFinishPickingImage : \n{\t%@\n}",editingInfo);
}

- (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
        NSLog(@"\ndidFinishPickingMediaWithInfo : \n{\t%@\n}",info);
}

- (void) imagePickerControllerDidCancel:(UIImagePickerController *)picker {
    NSLog(@"imagePickerControllerDidCancel");
}

@end


@interface AppDelegate : UIResponder <UIApplicationDelegate>

@property (strong, nonatomic) UIWindow *window;
@property (strong, nonatomic) UIViewController *viewController;

@end

@implementation AppDelegate

@synthesize window = _window;
@synthesize viewController = _viewController;

- (void) dealloc {
    [_window release];
    [_viewController release];
    
    [super dealloc];
}

- (BOOL) application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    
    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
    
    self.viewController = [[ViewController alloc] init];
    
    UINavigationController *controller = [[UINavigationController alloc] initWithRootViewController:self.viewController]; 
    self.window.rootViewController = controller;
    [controller release];
    
    [self.window makeKeyAndVisible];
    return YES;
}

@end

int main(int argc, char *argv[])
{
    @autoreleasepool {
        return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
    }
}


你可能感兴趣的:(1天学习1个类 UIImagePickerController 示例)