iOS中,iPhone和iPad调用系统的方法不相同,需要区分判断,否则会崩溃,错误提示如下:
Warning: Error creating LLDB target at path 'xxx'- using an empty LLDB target which can cause slow memory reads from remote devices.
1.我们可以使用系统的UI_USER_INTERFACE_IDIOM()去判断获取设备的类型,如下:
//iPhone设备
UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone
//ipad设备
UI_USER_INTERFACE_IDIOM()== UIUserInterfaceIdiomPad
2.在视图控制器上,iPhone使用UIImagePickerController图像选取器来调用系统相册,iPad则需使用UIPopoverController视图控制器
//iPhone
UIImagePickerController *picker;
//iPad
UIPopoverController *imagePicker;
完整示例代码
1.首先在.pch文件定义一个宏用于判断是什么类型设备(根据自身需要决定)
//iPhone设备
#define IS_IPHONE (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
//iPad设备
#define IS_PAD (UI_USER_INTERFACE_IDIOM()== UIUserInterfaceIdiomPad)
2.在调用的VC文件中
//添加代理
<UIImagePickerControllerDelegate,UINavigationControllerDelegate>
//声明函数
//iPhone
@property (strong, nonatomic) UIImagePickerController *picker;
//iPad
@property (nonatomic, retain) UIPopoverController *imagePicker;
//调用相册方法
-(void)getPhotoMethod
{
if (IS_IPHONE) {
NSLog(@"iPhone调用相册");
self.picker = [[UIImagePickerController alloc] init];
self.picker.delegate = self;
self.picker.allowsEditing = YES;
UIAlertController *actionSheet = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
UIAlertAction *cameraAction = [UIAlertAction actionWithTitle:@"从相机拍照" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action){
if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]){
self.picker.sourceType = UIImagePickerControllerSourceTypeCamera;
[self presentViewController:self.picker animated:YES completion:nil];
}
}];
UIAlertAction *photoAction = [UIAlertAction actionWithTitle:@"从相册选择" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
self.picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self presentViewController:self.picker animated:YES completion:nil];
}];
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:Localized(@"pr_key_16") style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
NSLog(@"取消");
}];
[actionSheet addAction:cameraAction];
[actionSheet addAction:photoAction];
[actionSheet addAction:cancelAction];
[self presentViewController:actionSheet animated:YES completion:nil];
}else{
NSLog(@"ipad调用相册");
UIImagePickerControllerSourceType sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
//是否允许编辑
picker.allowsEditing = NO;
picker.sourceType = sourceType;
UIPopoverController *popover = [[UIPopoverController alloc]initWithContentViewController:picker];
self.imagePicker = popover;
//permittedArrowDirections 设置箭头方向
[self.imagePicker presentPopoverFromRect:CGRectMake(0, 0, 300, 300) inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
}
}
/**
*实现代理
*/
//在这里处理获取到的图片
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info
{
// 获取图片
UIImage *image = info[UIImagePickerControllerOriginalImage];
[picker dismissViewControllerAnimated:YES completion:nil];
}
//点击取消按钮时调用
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
[picker dismissViewControllerAnimated:YES completion:nil];
}
3.最后,在info.plist文件中配置相应的权限,如:”Privacy - Camera Usage Description “、"Privacy - Photo Library Usage Description"等
网友评论