因項(xiàng)目后期可能需要備份通訊錄和相冊媳荒,趁現(xiàn)在時(shí)間多,提前先了解下寫個(gè)Demo貌夕。關(guān)于Contacts也有官方的視頻磷籍,但視頻是用Swift寫的闷哆,讓我也覺得要快速入手Swift了站楚。
通訊錄
通訊錄要先導(dǎo)入?#import <Contacts/Contacts.h>
添加聯(lián)系人(由于我主要做備份,就寫死了)
CNMutableContact * contact = [[CNMutableContact alloc]init];
contact.imageData = UIImagePNGRepresentation([UIImage imageNamed:@"naruto.jpg"]);
contact.givenName = @"steven";//設(shè)置名字
contact.familyName = @"guo";//設(shè)置姓氏
contact.emailAddresses = @[[CNLabeledValue labeledValueWithLabel:CNLabelWork value:@"956995511@qq.com"]];//郵箱
contact.phoneNumbers = @[[CNLabeledValue labeledValueWithLabel:CNLabelPhoneNumberiPhone value:[CNPhoneNumber phoneNumberWithStringValue:@"13999999999"]]];//電話
/*其余屬性我就不配置了
@property (copy, NS_NONATOMIC_IOSONLY) NSString *namePrefix;
@property (copy, NS_NONATOMIC_IOSONLY) NSString *givenName;
@property (copy, NS_NONATOMIC_IOSONLY) NSString *middleName;
@property (copy, NS_NONATOMIC_IOSONLY) NSString *familyName;
@property (copy, NS_NONATOMIC_IOSONLY) NSString *previousFamilyName;
@property (copy, NS_NONATOMIC_IOSONLY) NSString *nameSuffix;
@property (copy, NS_NONATOMIC_IOSONLY) NSString *nickname;
@property (copy, NS_NONATOMIC_IOSONLY) NSString *phoneticGivenName;
@property (copy, NS_NONATOMIC_IOSONLY) NSString *phoneticMiddleName;
@property (copy, NS_NONATOMIC_IOSONLY) NSString *phoneticFamilyName;
@property (copy, NS_NONATOMIC_IOSONLY) NSString *organizationName;
@property (copy, NS_NONATOMIC_IOSONLY) NSString *departmentName;
@property (copy, NS_NONATOMIC_IOSONLY) NSString *jobTitle;
@property (copy, NS_NONATOMIC_IOSONLY) NSString *note;
太多了轻绞,未完采记。。政勃。
*/
//地址
CNMutablePostalAddress * homeAdress = [[CNMutablePostalAddress alloc]init];
homeAdress.street = @"大街";
homeAdress.city = @"深圳";
homeAdress.state = @"中國";
homeAdress.postalCode = @"518000";
contact.postalAddresses = @[[CNLabeledValue labeledValueWithLabel:CNLabelHome value:homeAdress]];
//生日
NSDateComponents * birthday = [[NSDateComponents alloc]init];
birthday.day=4;
birthday.month=2;
birthday.year=1989;
contact.birthday=birthday;
CNSaveRequest * saveRequest = [[CNSaveRequest alloc]init];
//將創(chuàng)建的聯(lián)系人添加到系統(tǒng)通訊錄中
[saveRequest addContact:contact toContainerWithIdentifier:nil];
/*方法也有很多
//添加一個(gè)聯(lián)系人
- (void)addContact:(CNMutableContact *)contact toContainerWithIdentifier:(nullable NSString *)identifier;
//更新一個(gè)聯(lián)系人
- (void)updateContact:(CNMutableContact *)contact;
//刪除一個(gè)聯(lián)系人
- (void)deleteContact:(CNMutableContact *)contact;
//添加一組聯(lián)系人
- (void)addGroup:(CNMutableGroup *)group toContainerWithIdentifier:(nullable NSString *)identifier;
//更新一組聯(lián)系人
- (void)updateGroup:(CNMutableGroup *)group;
//刪除一組聯(lián)系人
- (void)deleteGroup:(CNMutableGroup *)group;
//向組中添加子組
- (void)addSubgroup:(CNGroup *)subgroup toGroup:(CNGroup *)group NS_AVAILABLE(10_11, NA);
//在組中刪除子組
- (void)removeSubgroup:(CNGroup *)subgroup fromGroup:(CNGroup *)group NS_AVAILABLE(10_11, NA);
//向組中添加成員
- (void)addMember:(CNContact *)contact toGroup:(CNGroup *)group;
//向組中移除成員
- (void)removeMember:(CNContact *)contact fromGroup:(CNGroup *)group;
*/
CNContactStore * store = [[CNContactStore alloc]init];
[store executeSaveRequest:saveRequest error:nil];//保存前面創(chuàng)建的請求
跳轉(zhuǎn)至系統(tǒng)通訊錄
需導(dǎo)入#import <ContactsUI/ContactsUI.h>
CNContactPickerViewController * contactVC = [[CNContactPickerViewController alloc]init];
[self presentViewController:contactVC animated:YES completion:nil];
比起之前的AddressBookFramework唧龄,這個(gè)簡單的不要不要的。奸远。既棺。
規(guī)檔
CNContactStore * stroe = [[CNContactStore alloc]init];
CNContactFetchRequest * request = [[CNContactFetchRequest alloc]initWithKeysToFetch:@[CNContactIdentifierKey,CNContactFamilyNameKey,CNContactGivenNameKey,CNContactOrganizationNameKey,CNContactPhoneNumbersKey,CNContactEmailAddressesKey,CNContactPostalAddressesKey,CNContactImageDataKey]];
NSMutableArray * arr = [@[] mutableCopy];
[stroe enumerateContactsWithFetchRequest:request error:nil usingBlock:^(CNContact * _Nonnull contact, BOOL * _Nonnull stop) {
[arr addObject:contact];
}];
[NSKeyedArchiver archiveRootObject:arr toFile:@"/Users/Crazy_Steven/Desktop/contact.xml"];//規(guī)檔到桌面
詳細(xì)的可以看Demo.
相冊
跳轉(zhuǎn)至系統(tǒng)相冊
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
[self presentViewController:picker animated:YES completion:nil];
備份相冊
使用Photos框架,先#import <Photos/Photos.h>
NSMutableArray *dataArray = [NSMutableArray array];
PHFetchOptions *fetchOptions = [[PHFetchOptions alloc]init];
PHFetchResult *smartAlbumsFetchResult = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeSmartAlbum subtype:PHAssetCollectionSubtypeSmartAlbumUserLibrary options:fetchOptions];
/*第一個(gè)參數(shù)的枚舉含義
PHAssetCollectionTypeAlbum //從 iTunes 同步來的相冊懒叛,以及用戶在 Photos 中自己建立的相冊
PHAssetCollectionTypeSmartAlbum //經(jīng)由相機(jī)得來的相冊
PHAssetCollectionTypeMoment //Photos 為我們自動生成的時(shí)間分組的相冊
*/
/*第二個(gè)參數(shù)的枚舉含義
PHAssetCollectionSubtypeAlbumRegular //用戶在 Photos 中創(chuàng)建的相冊丸冕,也就是我所謂的邏輯相冊
PHAssetCollectionSubtypeAlbumSyncedEvent //使用 iTunes 從 Photos 照片庫或者 iPhoto 照片庫同步過來的事件。然而薛窥,在iTunes 12 以及iOS 9.0 beta4上胖烛,選用該類型沒法獲取同步的事件相冊,而必須使用AlbumSyncedAlbum诅迷。
PHAssetCollectionSubtypeAlbumSyncedFaces //使用 iTunes 從 Photos 照片庫或者 iPhoto 照片庫同步的人物相冊佩番。
PHAssetCollectionSubtypeAlbumSyncedAlbum //做了 AlbumSyncedEvent 應(yīng)該做的事
PHAssetCollectionSubtypeAlbumImported //從相機(jī)或是外部存儲導(dǎo)入的相冊.
PHAssetCollectionSubtypeAlbumMyPhotoStream //用戶的 iCloud 照片流
PHAssetCollectionSubtypeAlbumCloudShared //用戶使用 iCloud 共享的相冊
PHAssetCollectionSubtypeSmartAlbumGeneric //文檔解釋為非特殊類型的相冊,主要包括從 iPhoto 同步過來的相冊罢杉。
PHAssetCollectionSubtypeSmartAlbumPanoramas //相機(jī)拍攝的全景照片
PHAssetCollectionSubtypeSmartAlbumVideos //相機(jī)拍攝的視頻
PHAssetCollectionSubtypeSmartAlbumFavorites //收藏文件夾
PHAssetCollectionSubtypeSmartAlbumTimelapses //延時(shí)視頻文件夾答捕,同時(shí)也會出現(xiàn)在視頻文件夾中
PHAssetCollectionSubtypeSmartAlbumAllHidden //包含隱藏照片或視頻的文件夾
PHAssetCollectionSubtypeSmartAlbumRecentlyAdded //相機(jī)近期拍攝的照片或視頻
PHAssetCollectionSubtypeSmartAlbumBursts //連拍模式拍攝的照片,在 iPad mini 上按住快門不放就可以了屑那,但是照片依然沒有存放在這個(gè)文件夾下拱镐,而是在相機(jī)相冊里。
PHAssetCollectionSubtypeSmartAlbumSlomoVideos //Slomo 是 slow motion 的縮寫持际,高速攝影慢動作解析沃琅,在該模式下,iOS 設(shè)備以120幀拍攝蜘欲。不過我的 iPad mini 不支持益眉,沒法驗(yàn)證。
PHAssetCollectionSubtypeSmartAlbumUserLibrary //相機(jī)相冊姥份,所有相機(jī)拍攝的照片或視頻都會出現(xiàn)在該相冊中郭脂,而且使用其他應(yīng)用保存的照片也會出現(xiàn)在這里。
PHAssetCollectionSubtypeAny //包含所有類型
*/
//這里說明一下澈歉,必須要同步執(zhí)行展鸡。。埃难。否則你還沒遍歷完圖片就規(guī)檔完了
dispatch_async(dispatch_get_global_queue(0, 0), ^{
//遍歷相機(jī)膠卷
[smartAlbumsFetchResult enumerateObjectsUsingBlock:^(PHAssetCollection * _Nonnull collection, NSUInteger idx, BOOL *stop) {
if (![collection.localizedTitle isEqualToString:@"Videos"]) {
NSArray<PHAsset *> *assets = [self GetAssetsInAssetCollection:collection];
for (PHAsset * asset in assets) {
[[PHImageManager defaultManager]requestImageDataForAsset:asset options:nil resultHandler:^(NSData * _Nullable imageData, NSString * _Nullable dataUTI, UIImageOrientation orientation, NSDictionary * _Nullable info) {
[dataArray addObject:imageData];
}];
}
}
}];
//遍歷自定義相冊
PHFetchResult *smartAlbumsFetchResult1 = [PHAssetCollection fetchTopLevelUserCollectionsWithOptions:fetchOptions];
[smartAlbumsFetchResult1 enumerateObjectsUsingBlock:^(PHAssetCollection * _Nonnull collection, NSUInteger idx, BOOL *stop) {
NSArray<PHAsset *> *assets = [self GetAssetsInAssetCollection:collection];
for (PHAsset * asset in assets) {
[[PHImageManager defaultManager]requestImageDataForAsset:asset options:nil resultHandler:^(NSData * _Nullable imageData, NSString * _Nullable dataUTI, UIImageOrientation orientation, NSDictionary * _Nullable info) {
[dataArray addObject:imageData];
}];
}
}];
[dataArray writeToFile:@"/Users/Crazy_Steven/Desktop/Photos.plist" atomically:YES];
});
- (NSArray *)GetAssetsInAssetCollection:(PHAssetCollection *)assetCollection
{
NSMutableArray<PHAsset *> *arr = [NSMutableArray array];
PHFetchResult *result = [self GetFetchResult:assetCollection];
[result enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
if (((PHAsset *)obj).mediaType == PHAssetMediaTypeImage) {
[arr addObject:obj];
}
}];
return arr;
}
-(NSString *) FormatPhotoAlumTitle:(NSString *)title
{
if ([title isEqualToString:@"All Photos"] || [title isEqualToString:@"Camera Roll"]) {
return @"相機(jī)膠卷";
}
return nil;
}
-(PHFetchResult *)GetFetchResult:(PHAssetCollection *)assetCollection
{
PHFetchResult *fetchResult = [PHAsset fetchAssetsInAssetCollection:assetCollection options:nil];
return fetchResult;
}
具體效果還是看Demo,和通訊錄的寫到一起了莹弊,用的方法比較Low,不知道百度之類的云盤是怎么實(shí)現(xiàn)備份的,知道的也請大家告訴我涡尘,一同進(jìn)步忍弛。。考抄。