这段利用苹果自带相机进行录像的代码对开发的应用中用到拍照、摄像功能的开发者应该有所帮助。
-(void) choosePhotoBySourceType: (UIImagePickerControllerCameraCaptureMode) sourceType
{
m_imagePickerController = [[[UIImagePickerController alloc] init] autorelease];
m_imagePickerController.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
m_imagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera;
m_imagePickerController.cameraDevice = UIImagePickerControllerCameraDeviceFront;
//m_imagePickerController.cameraCaptureMode = UIImagePickerControllerCameraCaptureModeVideo;
NSArray *sourceTypes = [UIImagePickerController availableMediaTypesForSourceType:m_imagePickerController.sourceType];
if ([sourceTypes containsObject:(NSString *)kUTTypeMovie ])
{
m_imagePickerController.mediaTypes= [NSArray arrayWithObjects:(NSString *)kUTTypeMovie,(NSString *)kUTTypeImage,nil];
}
// m_imagePickerController.cameraCaptureMode = sourceType;
//m_imagePickerController.mediaTypes
//imagePickerController.allowsEditing = YES;
[self presentModalViewController: m_imagePickerController animated:YES];
}
-(void) takePhoto
{
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
{
[self choosePhotoBySourceType:nil];
}
}
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
[super viewDidLoad];
UIButton *takePhoto = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[takePhoto setTitle:@"录像" forState:UIControlStateNormal];
[takePhoto addTarget:self action:@selector(takePhoto) forControlEvents:UIControlEventTouchUpInside];
takePhoto.frame = CGRectMake(50,100,100,30);
[self.view addSubview:takePhoto];
}
-(void) choosePhotoBySourceType: (UIImagePickerControllerCameraCaptureMode) sourceType
{
m_imagePickerController = [[[UIImagePickerController alloc] init] autorelease];
m_imagePickerController.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
m_imagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera;
m_imagePickerController.cameraDevice = UIImagePickerControllerCameraDeviceFront;
//m_imagePickerController.cameraCaptureMode = UIImagePickerControllerCameraCaptureModeVideo;
NSArray *sourceTypes = [UIImagePickerController availableMediaTypesForSourceType:m_imagePickerController.sourceType];
if ([sourceTypes containsObject:(NSString *)kUTTypeMovie ])
{
m_imagePickerController.mediaTypes= [NSArray arrayWithObjects:(NSString *)kUTTypeMovie,(NSString *)kUTTypeImage,nil];
}
// m_imagePickerController.cameraCaptureMode = sourceType;
//m_imagePickerController.mediaTypes
//imagePickerController.allowsEditing = YES;
[self presentModalViewController: m_imagePickerController animated:YES];
}
-(void) takePhoto
{
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
{
[self choosePhotoBySourceType:nil];
}
}
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
[super viewDidLoad];
UIButton *takePhoto = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[takePhoto setTitle:@"录像" forState:UIControlStateNormal];
[takePhoto addTarget:self action:@selector(takePhoto) forControlEvents:UIControlEventTouchUpInside];
takePhoto.frame = CGRectMake(50,100,100,30);
[self.view addSubview:takePhoto];
}