拍照、相册、通讯录

来源:互联网 发布:热仿真软件flotherm 编辑:程序博客网 时间:2024/06/17 10:29

一、拍照与相册

//相机

if ([UIImagePickerControllerisSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) 

{
            UIImagePickerController *pickController = [[UIImagePickerControlleralloc]init];
            pickController.sourceType =UIImagePickerControllerSourceTypeCamera;
            pickController.delegate =self;
            pickController.allowsEditing =YES;
            [selfpresentViewController:pickControlleranimated:YEScompletion:nil];
 }

//相册
if ([UIImagePickerControllerisSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary])
 {

            UIImagePickerController *pickController = [[UIImagePickerControlleralloc]init];
            pickController.sourceType =UIImagePickerControllerSourceTypePhotoLibrary;
            pickController.delegate =self;
            pickController.allowsEditing =YES;
            [selfpresentViewController:pickControlleranimated:YEScompletion:nil];
  }

//代理方法

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info

二、通讯录

// 1.创建选择联系人的控制器
    CNContactPickerViewController *contactVc = [[CNContactPickerViewControlleralloc]init];
    // 2.设置代理
    contactVc.delegate =self;
    // 3.弹出控制器
    [selfpresentViewController:contactVcanimated:YEScompletion:nil];

=====代理方法

- (void)contactPicker:(CNContactPickerViewController *)picker didSelectContact:(CNContact *)contact
{
    
    // 1.获取联系人的姓名
    NSString *lastname = contact.familyName;
    self.personlab.text = lastname;
   // 2.获取联系人的电话号码(此处获取的是该联系人的第一个号码,也可以遍历所有的号码)
    NSArray *phoneNums = contact.phoneNumbers;
    
    CNLabeledValue *labeledValue = phoneNums[0];
    
    CNPhoneNumber *phoneNumer = labeledValue.value;
    
    NSString *phone = phoneNumer.stringValue;
    NSString *str3 = [phonestringByReplacingOccurrencesOfString:@"-"withString:@""];
    self.tellab.text = str3;
}

三、通讯录加权限

步骤:
1.先导入头文件:#import <ContactsUI/ContactsUI.h>
2.遵守协议:CNContactPickerDelegate
3.获取访问通讯录的权限
4.有权限就访问
5.点击联系人进入详情页面,要再异步主队列进行,不然会报Time out error;

.m文件完整代码如下:

#import "ViewController.h"#import <ContactsUI/ContactsUI.h>@interface ViewController ()<CNContactPickerDelegate>@property (nonatomic , strong) CNContactPickerViewController *contactPicker;@end@implementation ViewController- (CNContactPickerViewController *)contactPicker{    if (_contactPicker == nil) {        _contactPicker = [[CNContactPickerViewController alloc] init];        _contactPicker.delegate = self;    }    return _contactPicker;}- (void)viewDidLoad {    [super viewDidLoad];    //创建CNContactStore对象,用与获取和保存通讯录信息    CNContactStore *contactStore = [[CNContactStore alloc] init];    if ([CNContactStore authorizationStatusForEntityType:CNEntityTypeContacts] == CNAuthorizationStatusNotDetermined) {        //首次访问通讯录会调用        [contactStore requestAccessForEntityType:CNEntityTypeContacts completionHandler:^(BOOL granted, NSError * _Nullable error) {            if (error) return;            if (granted) {//允许                NSLog(@"授权访问通讯录");                [self fetchContactWithContactStore:contactStore];//访问通讯录            }else{//拒绝                NSLog(@"拒绝访问通讯录");//访问通讯录            }          }];      }    else if([CNContactStore authorizationStatusForEntityType:CNEntityTypeContacts] == CNAuthorizationStatusAuthorized){       NSError *error = nil;        //创建数组,必须遵守CNKeyDescriptor协议,放入相应的字符串常量来获取对应的联系人信息        NSArray <id<CNKeyDescriptor>> *keysToFetch = @[CNContactFamilyNameKey, CNContactGivenNameKey, CNContactPhoneNumbersKey];        //创建获取联系人的请求        CNContactFetchRequest *fetchRequest = [[CNContactFetchRequest alloc] initWithKeysToFetch:keysToFetch];        //遍历查询        [contactStore enumerateContactsWithFetchRequest:fetchRequest error:&error usingBlock:^(CNContact * _Nonnull contact, BOOL * _Nonnull stop) {            if (!error) {                NSLog(@"familyName = %@", contact.familyName);//姓                NSLog(@"givenName = %@", contact.givenName);//名字                NSLog(@"phoneNumber = %@", ((CNPhoneNumber *)(contact.phoneNumbers.lastObject.value)).stringValue);//电话            }              else{                    NSLog(@"error:%@", error.localizedDescription);            }        }];        //调用通讯录        [self presentViewController:self.contactPicker  animated:YES completion:nil];    }   else{        //无权限访问        NSLog(@"拒绝访问通讯录");      }}#pragma  mark 访问通讯录- (void)fetchContactWithContactStore:(CNContactStore *)contactStore{    //调用通讯录    self.contactPicker.displayedPropertyKeys =@[CNContactEmailAddressesKey, CNContactBirthdayKey, CNContactImageDataKey];    [self presentViewController:self.contactPicker  animated:YES completion:nil];}#pragma mark 选择联系人进入详情- (void)contactPicker:(CNContactPickerViewController *)picker didSelectContact:(CNContact *)contact{    dispatch_async(dispatch_get_main_queue(), ^{        CNContactViewController *vc = [CNContactViewController viewControllerForContact:contact];        vc.displayedPropertyKeys =@[CNContactGivenNameKey,CNContactPhoneNumbersKey,CNContactFamilyNameKey,CNContactInstantMessageAddressesKey,CNContactEmailAddressesKey,CNContactDatesKey,CNContactUrlAddressesKey,CNContactBirthdayKey, CNContactImageDataKey];        [self presentViewController:vc animated:YES completion:nil];    });}@end


0 0