官方文档地址:Camera and Photo
两种方法:
- UIImagePickerController
- AV Foundation framework(暂时用不到)
UIImagePickerController
- info.plist中增加UIRequiredDeviceCapabilities
- 使用UIImagePickerController的isSourceTypeAvailable:方法判断相机是否可用
- 实现delegate
Sample Codes
if ([UIImagePickerController isSourceTypeAvailable:
UIImagePickerControllerSourceTypeCamera] == NO)
return NO;
UIImagePickerController *cameraUI = [[UIImagePickerController alloc] init];
cameraUI.sourceType = UIImagePickerControllerSourceTypeCamera;
NSArray* availableTypes = [UIImagePickerController availableMediaTypesForSourceType:
UIImagePickerControllerSourceTypeCamera];
if ([availableTypes containsObject:(NSString*)kUTTypeImage])
{
NSLog(@"Find target Media type.");
}
else
{
return NO;
}
cameraUI.mediaTypes = [NSArray arrayWithObject:(NSString*)kUTTypeImage];
cameraUI.allowsEditing = NO;
cameraUI.delegate = self;
[self presentViewController:cameraUI animated:YES completion:nil];