标题:
iOS截屏(视频黑色)并保存到自定义相册(2)
[打印本页]
作者:
look_w
时间:
2019-2-19 14:54
标题:
iOS截屏(视频黑色)并保存到自定义相册(2)
2.保存到自定义相册
导入photos框架
#pragma mark - 截图
- (void)screenshot{
DLog(@"jietu");
UIImage *image = [self captureCurrentView:self.view];
[self saveImageToPhotos:image];
}
#pragma mark - 保存图片
- (void)saveImageToPhotos
UIImage *)image {
//权限判断
PHAuthorizationStatus status = [PHPhotoLibrary authorizationStatus];
if (status == PHAuthorizationStatusDenied || status == PHAuthorizationStatusRestricted) {
[QYprogressHUD showInfoWithStatus
"未开启相册权限,请在系统设置中开启"];
return;
}else{
PHFetchResult<
HAsset *> *assets = [self syncSaveImageWithPhotos:image];//同步保存图片
if (assets == nil) {
[QYprogressHUD showErrorWithStatus
"保存失败"];
return;
}
PHAssetCollection * Album = [self creatAlbum];//创建相册
if (Album == nil) {
[QYprogressHUD showErrorWithStatus
"创建相册失败"];
return;
}
NSError *error = nil;
[[PHPhotoLibrary sharedPhotoLibrary] performChangesAndWait:^{
//--告诉系统,要操作哪个相册
PHAssetCollectionChangeRequest *collectionChangeRequest = [PHAssetCollectionChangeRequest changeRequestForAssetCollection:Album];
//--添加图片到自定义相册--追加--就不能成为封面了
//--[collectionChangeRequest addAssets:assets];
//--插入图片到自定义相册--插入--可以成为封面
[collectionChangeRequest insertAssets:assets atIndexes:[NSIndexSet indexSetWithIndex:0]];
} error:&error];
if (error) {
[QYprogressHUD showErrorWithStatus
"保存失败"];
}else
[QYprogressHUD showSuccessWithStatus
"保存成功"];
}
// UIImageWriteToSavedPhotosAlbum(image, self, @selector(image:didFinishSavingWithError:contextInfo
,nil);//保存到系统相册
}
/**同步方式保存图片到系统的相机胶卷中---返回的是当前保存成功后相册图片对象集合*/
-(PHFetchResult<
HAsset *> *)syncSaveImageWithPhotos
UIImage *)image
{
//--1 创建 ID 这个参数可以获取到图片保存后的 asset对象
__block NSString *createdAssetID = nil;
//--2 保存图片
NSError *error = nil;
[[PHPhotoLibrary sharedPhotoLibrary] performChangesAndWait:^{
//----block 执行的时候还没有保存成功--获取占位图片的 id,通过 id 获取图片---同步
createdAssetID = [PHAssetChangeRequest creationRequestForAssetFromImage:image].placeholderForCreatedAsset.localIdentifier;
} error:&error];
//--3 如果失败,则返回空
if (error) {
return nil;
}
//--4 成功后,返回对象
//获取保存到系统相册成功后的 asset 对象集合,并返回
PHFetchResult<
HAsset *> *assets = [PHAsset fetchAssetsWithLocalIdentifiers
[createdAssetID] options:nil];
return assets;
}
- (PHAssetCollection *)creatAlbum{
NSString *title = [NSBundle mainBundle].infoDictionary[(__bridge NSString *)kCFBundleNameKey];
//2 获取与 APP 同名的自定义相册
PHFetchResult<
HAssetCollection *> *collections = [PHAssetCollection fetchAssetCollectionsWithType
HAssetCollectionTypeAlbum subtype
HAssetCollectionSubtypeAlbumRegular options:nil];
for (PHAssetCollection *collection in collections) {
//遍历
if ([collection.localizedTitle isEqualToString:title]) {
//找到了同名的自定义相册--返回
return collection;
}
}
//说明没有找到,需要创建
NSError *error = nil;
__block NSString *createID = nil; //用来获取创建好的相册
[[PHPhotoLibrary sharedPhotoLibrary] performChangesAndWait:^{
//发起了创建新相册的请求,并拿到ID,当前并没有创建成功,待创建成功后,通过 ID 来获取创建好的自定义相册
PHAssetCollectionChangeRequest *request = [PHAssetCollectionChangeRequest creationRequestForAssetCollectionWithTitle:title];
createID = request.placeholderForCreatedAssetCollection.localIdentifier;
} error:&error];
if (error) {
[QYprogressHUD showErrorWithStatus
"创建失败"];
return nil;
}else{
[QYprogressHUD showSuccessWithStatus
"创建成功"];
//通过 ID 获取创建完成的相册 -- 是一个数组
return [PHAssetCollection fetchAssetCollectionsWithLocalIdentifiers
[createID] options:nil].firstObject;
}
}
权限
PHAuthorizationStatusNotDetermined ,---用户之前还未决定
PHAuthorizationStatusRestricted, ---系统问题,用户没有权限决定--比如家长控制器模式
PHAuthorizationStatusDenied,---用户之前拒绝过
PHAuthorizationStatusAuthorized --用户允许
欢迎光临 电子技术论坛_中国专业的电子工程师学习交流社区-中电网技术论坛 (http://bbs.eccn.com/)
Powered by Discuz! 7.0.0