1.截屏
用方法1正常,用方法2视频黑色.
当然你要确定view是不是最底层的view.
区别查到再补充,先做个记录
- (UIImage *)captureCurrentView:(UIView *)view {
方法1:(iOS7后替换方法2了)
UIGraphicsBeginImageContextWithOptions(view.frame.size, NO, 0.0);
[view drawViewHierarchyInRect:view.frame afterScreenUpdates:YES];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
方法2:
// // 创建一个context
// UIGraphicsBeginImageContextWithOptions(view.bounds.size, NO, 0.0);
//
// //把当前的全部画面导入到栈顶context中并进行渲染
// [view.layer renderInContext:UIGraphicsGetCurrentContext()];
//
// // 从当前context中创建一个新图片
// UIImage * img = UIGraphicsGetImageFromCurrentImageContext();
//
// // 使当前的context出堆栈
// UIGraphicsEndImageContext();
// return img;
}
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<PHAsset *> *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<PHAsset *> *)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<PHAsset *> *assets = [PHAsset fetchAssetsWithLocalIdentifiers:@[createdAssetID] options:nil];
return assets;
}
- (PHAssetCollection *)creatAlbum{
NSString *title = [NSBundle mainBundle].infoDictionary[(__bridge NSString *)kCFBundleNameKey];
//2 获取与 APP 同名的自定义相册
PHFetchResult<PHAssetCollection *> *collections = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeAlbum subtype:PHAssetCollectionSubtypeAlbumRegular 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://www.jianshu.com/p/43ae2a98147f ,我谢谢你
3.保存到系统相册
// UIImageWriteToSavedPhotosAlbum(image, self, @selector(image:didFinishSavingWithError:contextInfo:),nil);//保存到系统相册
//- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo {
// if (error == nil) {
// NSLog(@"保存成功");
// [QYprogressHUD showSuccessWithStatus:@"截图以保存到相册"];
// } else {
// NSLog(@"失败");
// [QYprogressHUD showErrorWithStatus:@"截图失败"];
// }
//}
4.知识点补充
4.1首次判断相册权限
PHAuthorizationStatus status = [PHPhotoLibrary authorizationStatus];
if (status == PHAuthorizationStatusNotDetermined) {
[PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus status) {
if (status == PHAuthorizationStatusAuthorized) {//同意
[weakSelf canSaveImg:image];
return ;
}else
{
[QYprogressHUD showInfoWithStatus:@"未获得相册权限,截屏保存失败"];
return ;
}
}];
}
待续...
网友评论