需要導(dǎo)入的頭文件
#import <Photos/Photos.h>
#import <SVProgressHUD.h>
self的兩個(gè)屬性
@property (nonatomic, weak) UIImageView *imageView;
@property (weak, nonatomic) IBOutlet UIButton *saveButton;
創(chuàng)建的相簿的名字
static NSString * assetCollectionTitle = @"截圖相簿";
然后實(shí)現(xiàn)下面的幾個(gè)方法就可以了:
這是保存按鈕的點(diǎn)擊事件
- (IBAction)save {
/*
PHAuthorizationStatusNotDetermined, 用戶還沒(méi)有做出選擇
PHAuthorizationStatusDenied, 用戶拒絕當(dāng)前應(yīng)用訪問(wèn)相冊(cè)(用戶當(dāng)初點(diǎn)擊了"不允許")
PHAuthorizationStatusAuthorized 用戶允許當(dāng)前應(yīng)用訪問(wèn)相冊(cè)(用戶當(dāng)初點(diǎn)擊了"好")
PHAuthorizationStatusRestricted, 因?yàn)榧议L(zhǎng)控制, 導(dǎo)致應(yīng)用無(wú)法方法相冊(cè)(跟用戶的選擇沒(méi)有關(guān)系)
*/
// 判斷授權(quán)狀態(tài)
PHAuthorizationStatus status = [PHPhotoLibrary authorizationStatus];
if (status == PHAuthorizationStatusRestricted) { // 因?yàn)榧议L(zhǎng)控制, 導(dǎo)致應(yīng)用無(wú)法方法相冊(cè)(跟用戶的選擇沒(méi)有關(guān)系)
[SVProgressHUD showErrorWithStatus:@"因?yàn)橄到y(tǒng)原因, 無(wú)法訪問(wèn)相冊(cè)"];
} else if (status == PHAuthorizationStatusDenied) { // 用戶 當(dāng)前應(yīng)用訪問(wèn)相冊(cè)(用戶當(dāng)初點(diǎn)擊了"不允許")
FRLog(@"提醒用戶去[設(shè)置-隱私-照片-xxx]打開訪問(wèn)開關(guān)");
} else if (status == PHAuthorizationStatusAuthorized) { // 用戶允許當(dāng)前應(yīng)用訪問(wèn)相冊(cè)(用戶當(dāng)初點(diǎn)擊了"好")
[self saveImage];
} else if (status == PHAuthorizationStatusNotDetermined) { // 用戶還沒(méi)有做出選擇
// 彈框請(qǐng)求用戶授權(quán)
[PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus status) {
if (status == PHAuthorizationStatusAuthorized) { // 用戶點(diǎn)擊了好
[self saveImage];
}
}];
}
}
保存圖片
- (void)saveImage
{
// PHAsset : 一個(gè)資源, 比如一張圖片\一段視頻
// PHAssetCollection : 一個(gè)相簿
// PHAsset的標(biāo)識(shí), 利用這個(gè)標(biāo)識(shí)可以找到對(duì)應(yīng)的PHAsset對(duì)象(圖片對(duì)象)
__block NSString *assetLocalIdentifier = nil;
// 如果想對(duì)"相冊(cè)"進(jìn)行修改(增刪改), 那么修改代碼必須放在[PHPhotoLibrary sharedPhotoLibrary]的performChanges方法的block中
[[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{
// 1.保存圖片A到"相機(jī)膠卷"中
// 創(chuàng)建圖片的請(qǐ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 createAssetCollection];
if (createdAssetCollection == nil) {
[self showError:@"創(chuàng)建相簿失敗"];
}
[[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{
// 3.添加"相機(jī)膠卷"中的圖片A到"相簿"D中
// 獲得圖片
PHAsset *asset = [PHAsset fetchAssetsWithLocalIdentifiers:@[assetLocalIdentifier] options:nil].lastObject;
// 添加圖片到相簿中的請(qǐng)求
PHAssetCollectionChangeRequest *request = [PHAssetCollectionChangeRequest changeRequestForAssetCollection:createdAssetCollection];
// 添加圖片到相簿
[request addAssets:@[asset]];
} completionHandler:^(BOOL success, NSError * _Nullable error) {
if (success == NO) {
[self showError:@"保存圖片失敗"];
return ;
} else {
[self showSuccess:@"保存圖片成功"];
}
}];
}];
}
獲得手機(jī)中的相簿浓恳,如果之前沒(méi)有存在就會(huì)創(chuàng)建一個(gè)
/** 獲得相簿 */
- (PHAssetCollection *)createAssetCollection{
// 從已存在相簿中查找這個(gè)應(yīng)用對(duì)應(yīng)的相簿
PHFetchResult<PHAssetCollection *> *assetCollections = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeAlbum subtype:PHAssetCollectionSubtypeAlbumRegular options:nil];
for (PHAssetCollection *assetCollection in assetCollections) {
if ([assetCollection.localizedTitle isEqualToString:FRAssetCollectionTitle]) {
return assetCollection;
}
}
// 沒(méi)有找到對(duì)應(yīng)的相簿茸歧,得創(chuàng)建新的相簿 ---------------
// 錯(cuò)誤信息
NSError *error = nil;
// PHAssetCollection的標(biāo)識(shí), 利用這個(gè)標(biāo)識(shí)可以找到對(duì)應(yīng)的PHAssetCollection對(duì)象(相簿對(duì)象)
__block NSString *assetCollectionLocalIdentifier = nil;
[[PHPhotoLibrary sharedPhotoLibrary] performChangesAndWait:^{
// 創(chuàng)建相簿的請(qǐng)求
assetCollectionLocalIdentifier = [PHAssetCollectionChangeRequest creationRequestForAssetCollectionWithTitle:FRAssetCollectionTitle].placeholderForCreatedAssetCollection.localIdentifier;
} error:&error];
// 如果有錯(cuò)誤信息
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];
});
}
在項(xiàng)目的適當(dāng)?shù)胤郊由弦陨系拇a就能實(shí)現(xiàn)保存圖片到手機(jī)相簿中了