导入库Photos
1,首先请求权限:
-(void)requestAuthorization {
[PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus status) {
//用户同意则开始创建。不同意弹出提示
if (status == PHAuthorizationStatusAuthorized) {
[self createFolder];
} else {
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"App无法访问你的媒体资料库" message:@"app无法正常使用,现在去设置?" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *actionCancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
}];
[alert addAction:actionCancel];
UIAlertAction *actionOK = [UIAlertAction actionWithTitle:@"设置" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
NSURL * url = [NSURL URLWithString: UIApplicationOpenSettingsURLString];
if ( [[UIApplication sharedApplication] canOpenURL: url] ) {
NSURL*url = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
[[UIApplication sharedApplication] openURL:url];
}
}];
[alert addAction:actionOK];
dispatch_async(dispatch_get_main_queue(), ^{
[self presentViewController:alert animated:YES completion:nil];
});
}
}];
}
2,创建
由于相册名字用户是可以更改的,所以创建成功后应将相册的localIdentifier存储在本地,以便判断读取 。
- (void)createFolder {
if(GetAlbumID) {
return;
}
__block NSString *localIdentifier = nil;
[[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{
PHAssetCollectionChangeRequest *req = [PHAssetCollectionChangeRequest creationRequestForAssetCollectionWithTitle:VideoAlbumName];
localIdentifier = req.placeholderForCreatedAssetCollection.localIdentifier;
} completionHandler:^(BOOL success, NSError * _Nullable error) {
if (success) {
NSLog(@"创建相册文件夹成功!");
if (localIdentifier) {
[[NSUserDefaults standardUserDefaults] setObject:localIdentifier forKey:@"AlbumID"];
}
} else {
NSLog(@"创建相册文件夹失败:%@", error);
}
}];
}
3,保存
+(void)save:(NSURL *)url finish:(SaveAlbumFinish)saveAlbumFinish {
__block BOOL isHaveAlbum = NO;
//首先获取相册的集合
PHFetchResult *collectonResuts = [PHCollectionList fetchTopLevelUserCollectionsWithOptions:nil];
//对获取到集合进行遍历
[collectonResuts enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
PHAssetCollection *assetCollection = obj;
//根据GetAlbumID判断是否是我们要写入照片的相册
if ([assetCollection.localIdentifier isEqualToString:GetAlbumID]) {
isHaveAlbum = YES;
[[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{
//请求创建一个Asset
PHAssetChangeRequest *assetRequest = [PHAssetChangeRequest creationRequestForAssetFromVideoAtFileURL:url];
//请求编辑相册
PHAssetCollectionChangeRequest *collectonRequest = [PHAssetCollectionChangeRequest changeRequestForAssetCollection:assetCollection];
//为Asset创建一个占位符,放到相册编辑请求中
PHObjectPlaceholder *placeHolder = [assetRequest placeholderForCreatedAsset];
//相册中添加视频
[collectonRequest addAssets:@[placeHolder]];
} completionHandler:^(BOOL success, NSError *error) {
dispatch_async(dispatch_get_main_queue(), ^{
if (saveAlbumFinish) {
saveAlbumFinish(success, error);
}
});
}];
}
}];
//没有读取到待存储的相册
if (!isHaveAlbum) {
if (saveAlbumFinish) {
saveAlbumFinish(NO, [NSError errorWithDomain:@"没有相册" code:0 userInfo: nil]);
}
}
}
网友评论