1.保存圖片---判斷當前狀態(tài)(用戶是否允許訪問相冊)
- (IBAction)save:(UIButton *)sender {
// 判斷授權狀態(tài)
PHAuthorizationStatus status = [PHPhotoLibrary authorizationStatus];
if (status == PHAuthorizationStatusRestricted) { // 因為家長控制, 導致應用無法方法相冊(跟用戶的選擇沒有關系)
[SVProgressHUD showErrorWithStatus:@"因為系統(tǒng)原因, 無法訪問相冊"];
} else if (status == PHAuthorizationStatusDenied) { // 用戶拒絕當前應用訪問相冊(用戶當初點擊了"不允許")
NSLog(@"提醒用戶去[設置-隱私-照片-xxx]打開訪問開關");
} else if (status == PHAuthorizationStatusAuthorized) { // 用戶允許當前應用訪問相冊(用戶當初點擊了"好")
[self saveImage];
} else if (status == PHAuthorizationStatusNotDetermined) { // 用戶還沒有做出選擇
// 彈框請求用戶授權
[PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus status) {
if (status == PHAuthorizationStatusAuthorized) { // 用戶點擊了好
[self saveImage];
}
}];
}
}
(void)saveImage
{
// PHAsset : 一個資源, 比如一張圖片\一段視頻
// PHAssetCollection : 一個相簿
// PHAsset的標識, 利用這個標識可以找到對應的PHAsset對象(圖片對象)
__block NSString *assetLocalIdentifier = nil;
// 如果想對"相冊"進行修改(增刪改), 那么修改代碼必須放在[PHPhotoLibrary sharedPhotoLibrary]的performChanges方法的block中
[[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{
// 1.保存圖片A到"相機膠卷"中
// 創(chuàng)建圖片的請求
assetLocalIdentifier = [PHAssetCreationRequest creationRequestForAssetFromImage:self.imageView.image].placeholderForCreatedAsset.localIdentifier;
} completionHandler:^(BOOL success, NSError * _Nullable error) {
if (success == NO) {
[self showError:@"保存圖片失敗!"];
return;
}
2.獲得相簿
PHAssetCollection *createdAssetCollection = [self createdAssetCollection];
if (createdAssetCollection == nil) {
[self showError:@"創(chuàng)建相簿失敗!"];
return;
}
[[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{
// 3.添加"相機膠卷"中的圖片A到"相簿"D中
// 獲得圖片
PHAsset *asset = [PHAsset fetchAssetsWithLocalIdentifiers:@[assetLocalIdentifier] options:nil].lastObject;
// 添加圖片到相簿中的請求
PHAssetCollectionChangeRequest *request = [PHAssetCollectionChangeRequest changeRequestForAssetCollection:createdAssetCollection];
// 添加圖片到相簿
[request addAssets:@[asset]];
} completionHandler:^(BOOL success, NSError * _Nullable error) {
if (success == NO) {
[self showError:@"保存圖片失敗!"];;
} else {
[self showSuccess:@"保存圖片成功!"];;
}
}];
}];
}
-(PHAssetCollection *)createdAssetCollection
{
// 從已存在相簿中查找這個應用對應的相簿
PHFetchResult<PHAssetCollection *> *assetCollections = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeAlbum subtype:PHAssetCollectionSubtypeAlbumRegular options:nil];
for (PHAssetCollection *assetCollection in assetCollections) {
if ([assetCollection.localizedTitle isEqualToString:XMGAssetCollectionTitle]) {
return assetCollection;
}
}
// 沒有找到對應的相簿, 得創(chuàng)建新的相簿
// 錯誤信息
NSError *error = nil;
// PHAssetCollection的標識, 利用這個標識可以找到對應的PHAssetCollection對象(相簿對象)
__block NSString *assetCollectionLocalIdentifier = nil;
[[PHPhotoLibrary sharedPhotoLibrary] performChangesAndWait:^{
// 創(chuàng)建相簿的請求
assetCollectionLocalIdentifier = [PHAssetCollectionChangeRequest creationRequestForAssetCollectionWithTitle:XMGAssetCollectionTitle].placeholderForCreatedAssetCollection.localIdentifier;
} error:&error];
// 如果有錯誤信息
if (error) return nil;
// 獲得剛才創(chuàng)建的相簿
return [PHAssetCollection fetchAssetCollectionsWithLocalIdentifiers:@[assetCollectionLocalIdentifier] options:nil].lastObject;
}
- (void)showSuccess:(NSString *)text
{
dispatch_async(dispatch_get_main_queue(), ^{
[SVProgressHUD showSuccessWithStatus:text];
});
}
- (void)showError:(NSString *)text
{
dispatch_async(dispatch_get_main_queue(), ^{
[SVProgressHUD showErrorWithStatus:text];
});
}
最后編輯于 :
?著作權歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者