iOS Camera and Photo Library

官方文档地址:Camera and Photo

两种方法:

  1. UIImagePickerController
  2. AV Foundation framework(暂时用不到)

UIImagePickerController

  1. info.plist中增加UIRequiredDeviceCapabilities
  2. 使用UIImagePickerController的isSourceTypeAvailable:方法判断相机是否可用
  3. 实现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];

你可能感兴趣的:(iOS Camera and Photo Library)