#import "XMGSeeBigPictureViewController.h"
#import "XMGTopic.h"
#import <SVProgressHUD.h>
#import <Photos/Photos.h> @interface XMGSeeBigPictureViewController () <UIScrollViewDelegate>
@property (weak, nonatomic) IBOutlet UIButton *saveButton;
@property (nonatomic, weak) UIScrollView *scrollView;
@property (nonatomic, weak) UIImageView *imageView; /** 当前App对应的自定义相册 */
- (PHAssetCollection *)createdCollection;
/** 返回刚才保存到【相机胶卷】的图片 */
- (PHFetchResult<PHAsset *> *)createdAssets;
@end @implementation XMGSeeBigPictureViewController - (void)viewDidLoad {
[super viewDidLoad]; // scrollView
UIScrollView *scrollView = [[UIScrollView alloc] init];
scrollView.frame = [UIScreen mainScreen].bounds;
[scrollView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(back)]];
[self.view insertSubview:scrollView atIndex:0];
self.scrollView = scrollView; // imageView
UIImageView *imageView = [[UIImageView alloc] init];
[imageView sd_setImageWithURL:[NSURL URLWithString:self.topic.image1] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
if (!image) return;
self.saveButton.enabled = YES;
}];
imageView.xmg_width = scrollView.xmg_width;
imageView.xmg_height = imageView.xmg_width * self.topic.height / self.topic.width;
imageView.xmg_x = 0;
if (imageView.xmg_height > XMGScreenH) { // 超过一个屏幕
imageView.xmg_y = 0;
scrollView.contentSize = CGSizeMake(0, imageView.xmg_height);
} else {
imageView.xmg_centerY = scrollView.xmg_height * 0.5;
}
[scrollView addSubview:imageView];
self.imageView = imageView; // 图片缩放
CGFloat maxScale = self.topic.width / imageView.xmg_width;
if (maxScale > 1) {
scrollView.maximumZoomScale = maxScale;
scrollView.delegate = self;
}
} #pragma mark - <UIScrollViewDelegate>
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
return self.imageView;
} #pragma mark - 获得当前App对应的自定义相册
- (PHAssetCollection *)createdCollection
{
// 获得软件名字
NSString *title = [NSBundle mainBundle].infoDictionary[(__bridge NSString *)kCFBundleNameKey]; // 抓取所有的自定义相册
PHFetchResult<PHAssetCollection *> *collections = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeAlbum subtype:PHAssetCollectionSubtypeAlbumRegular options:nil]; // 查找当前App对应的自定义相册
for (PHAssetCollection *collection in collections) {
if ([collection.localizedTitle isEqualToString:title]) {
return collection;
}
} /** 当前App对应的自定义相册没有被创建过 **/
// 创建一个【自定义相册】
NSError *error = nil;
__block NSString *createdCollectionID = nil;
[[PHPhotoLibrary sharedPhotoLibrary] performChangesAndWait:^{
createdCollectionID = [PHAssetCollectionChangeRequest creationRequestForAssetCollectionWithTitle:title].placeholderForCreatedAssetCollection.localIdentifier;
} error:&error]; if (error) return nil; // 根据唯一标识获得刚才创建的相册
return [PHAssetCollection fetchAssetCollectionsWithLocalIdentifiers:@[createdCollectionID] options:nil].firstObject;
} - (PHFetchResult<PHAsset *> *)createdAssets
{
NSError *error = nil;
__block NSString *assetID = nil; // 保存图片到【相机胶卷】
[[PHPhotoLibrary sharedPhotoLibrary] performChangesAndWait:^{
assetID = [PHAssetChangeRequest creationRequestForAssetFromImage:self.imageView.image].placeholderForCreatedAsset.localIdentifier;
} error:&error]; if (error) return nil; // 获取刚才保存的相片
return [PHAsset fetchAssetsWithLocalIdentifiers:@[assetID] options:nil];
} #pragma mark - 监听点击
- (IBAction)back {
[self dismissViewControllerAnimated:YES completion:nil];
} - (IBAction)save {
PHAuthorizationStatus oldStatus = [PHPhotoLibrary authorizationStatus]; // 请求\检查访问权限 :
// 如果用户还没有做出选择,会自动弹框,用户对弹框做出选择后,才会调用block
// 如果之前已经做过选择,会直接执行block
[PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus status) {
dispatch_async(dispatch_get_main_queue(), ^{
if (status == PHAuthorizationStatusDenied) { // 用户拒绝当前App访问相册
if (oldStatus != PHAuthorizationStatusNotDetermined) {
XMGLog(@"提醒用户打开开关")
}
} else if (status == PHAuthorizationStatusAuthorized) { // 用户允许当前App访问相册
[self saveImageIntoAlbum];
} else if (status == PHAuthorizationStatusRestricted) { // 无法访问相册
[SVProgressHUD showErrorWithStatus:@"因系统原因,无法访问相册!"];
}
});
}];
} /**
* 保存图片到相册
*/
- (void)saveImageIntoAlbum
{
// 获得相片
PHFetchResult<PHAsset *> *createdAssets = self.createdAssets;
if (createdAssets == nil) {
[SVProgressHUD showErrorWithStatus:@"保存图片失败!"];
return;
} // 获得相册
PHAssetCollection *createdCollection = self.createdCollection;
if (createdCollection == nil) {
[SVProgressHUD showErrorWithStatus:@"创建或者获取相册失败!"];
return;
} // 添加刚才保存的图片到【自定义相册】
NSError *error = nil;
[[PHPhotoLibrary sharedPhotoLibrary] performChangesAndWait:^{
PHAssetCollectionChangeRequest *request = [PHAssetCollectionChangeRequest changeRequestForAssetCollection:createdCollection];
[request insertAssets:createdAssets atIndexes:[NSIndexSet indexSetWithIndex:0]];
} error:&error]; // 最后的判断
if (error) {
[SVProgressHUD showErrorWithStatus:@"保存图片失败!"];
} else {
[SVProgressHUD showSuccessWithStatus:@"保存图片成功!"];
}
}
@end

ios---photo实现保存图片到自定义相册的更多相关文章

  1. iOS中怎么存储照片到自定义相册

    在市场上主流App中,大多数App都具有存储图片到自己App的相册中.苹果提供的方法只能存储图片到系统相册,下面讲一下怎么实现: 实现思路:  1.对系统相册进行操作的前提必须导入#import &l ...

  2. iOS分段选择器、旅行App、标度尺、对对碰小游戏、自定义相册等源码

    iOS精选源码 企业级开源项目,模仿艺龙旅行App 标签选择器--LeeTagView CSSegmentedControl常用的分段选择器,简单易用! 仿微信左滑删除 IOS左滑返回 输入框 iOS ...

  3. iOS--app自定义相册--创建相簿,存储图片到手机

    我们在APP中点击照片,都会显示出大图,然后在大图的上面会有个保存照片的按钮,照片直接保存到了系统的相册中,但是因为公司产品的需要,让你创建和APP同名的相册保存在里面,那么就对了,可以看下具体的代码 ...

  4. app保存图片到用户相册时闪退解决办法

    在iOS11中,app保存图片到用户相册时必须添加权限使用描述即NSPhotoLibraryAddUsageDescription,否则会闪退. 只需在info.plist—Property List ...

  5. 微信小程序点击保存图片到本地相册——踩坑

    在微信小程序中要保存图片到本地相册,需要获取相册权限. 总之整个功能实现下来需要如下几个小程序的API:wx.getSetting,wx.authorize,wx.openSetting,wx.dow ...

  6. uniapp+nvue实现仿微信/得物相册插件:选择界面 +自定义相册+图片视频过滤

    本篇文章基于uniapp 框架+ nvue,实现了uniapp仿微信/得物相册选择功能实例项目,该插件实例实现了以下功能: 1: 相册过滤 2: 图视频过滤 3: 界面UI定制化 4: 栅格列数定制化 ...

  7. iOS开发UI篇—Quartz2D(自定义UIImageView控件)

    iOS开发UI篇—Quartz2D(自定义UIImageView控件) 一.实现思路 Quartz2D最大的用途在于自定义View(自定义UI控件),当系统的View不能满足我们使用需求的时候,自定义 ...

  8. android自定义相册 支持低端机不内存溢出

    1 之前在网上看的自定义相册很多时候在低端机都会内存溢出开始上代码把 首先我们要拿到图片的所有路径 cursor = context.getContentResolver().query( Media ...

  9. 借助TZImagePickerController三方库理解自定义相册

    借助TZImagePickerController三方库理解自定义相册 1.整体架构分析 整体框架大致可以分为几个部分 <1>工具类-TZImageManager:这个类主要是工作是提供一 ...

随机推荐

  1. python实现单词本功能

    #实现简单的单词本:# 可以添加单词和词义,当所添加的单词已经存在 让用户知道:# 查找单词,单词不存在时,让用户知道# 删除单词,当删除的单词不存在时,让用户知道# 以上功能无限制操作,直到用户输入 ...

  2. python 处理xml文件

    需求 在实际应用中,需要对xml配置文件进行实时修改, 1.增加.删除 某些节点 2.增加,删除,修改某个节点下的某些属性 3.增加,删除,修改某些节点的文本 <annotation> & ...

  3. Unity3D小游戏开发之两个我踩过的坑

    最近在开发一个植物大战僵尸小游戏,今天写了一早上的代码,踩了两个坑,这两个坑的位置分别位于触发器和数据转换,写这篇博文以此来让其他程序员不要再去踩这两个坑. 1.我在做简易僵尸模型的时候,这个僵尸模型 ...

  4. 洛谷$P2604\ [ZJOI2010]$网络扩容 网络流

    正解:网络流 解题报告: 传送门$QwQ$ 昂第一问跑个最大流就成不说$QwQ$ 然后第二问,首先原来剩下的边就成了费用为0的边?然后原来的所有边连接的两点都给加上流量为$inf$费用为$w$的边,保 ...

  5. jetbrains全家桶 你懂得

    这个是松哥说的引用一下: 昨天一直在忙,中午抽空瞅了一眼技术群,天呐,竟然都在切磋 IDEA 激活码的事情,瞬间明白可能 jetbrains 又在搞事情了. 我大概了解了下,这次出事的主要是 2019 ...

  6. Linux系统下 docker安装命令

    centos sudo: 使用 root 权限登录 Centos.确保 yum 包更新到最新.yum update 卸载旧版本(如果安装过旧版本的话)yum remove docker docker- ...

  7. Go合集,gRPC源码分析,算法合集

    年初时,朋友圈见到的最多的就是新的一年新的FlAG,年末时朋友圈最多的也是xxxx就要过去了,你的FLAG实现了吗? 这个公众号2016就已经创建了,但截至今年之前从来没发表过文章,现在想想以前很忙, ...

  8. 微信小程序订阅消息,我踩过的坑都在这里了!

    旧的模板消息将在 2020 年 1 月 10 号全面下架,也就是今天,不过貌似现在还可以用!!!我已经改好了,只不过还没有上线,准备坚持到最后一天! 0.订阅消息 简单介绍一下订阅消息的特点: 用户授 ...

  9. centos7 安装jdk8和maven3

    centos7 安装jdk8和maven3 标签(空格分隔): java,linux jdk 第一种安装: yum install java-1.8.0-openjdk /usr/lib/jvm/ 默 ...

  10. docker制作cenos+php56+nginx镜像

    首先你环境要安装好docker 1 获取centos镜像. docker search centos 选取第一个官方镜像. docker pull  docker.io/centos 新建镜像挂载目录 ...