美文网首页
保存图片到自定义胶卷

保存图片到自定义胶卷

作者: 米亚流年 | 来源:发表于2018-07-04 15:41 被阅读0次

pragma mark - 获取当前app对应的自定义相册

-(PHAssetCollection *) createdCollection{
//获得plist.info中的软件名称
NSString *title = [NSBundle mainBundle] .infoDictionary[@"CFBundleName"];

//抓取所有的自定义相册
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;
}

pragma mark - 获取图片到【相机胶卷】

-(PHFetchResult<PHAsset *> *) createdAssets{
NSError *error;
__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)save:(UIButton *)sender {
//取出当前授权状态
PHAuthorizationStatus oldStatus = [PHPhotoLibrary authorizationStatus];
//请求\检查访问权限
//1、如果用户还没做出选择,会自动弹框。用户对弹窗做出选择后,才会调用block。
//2、如果之前已经做过选择,会直接执行block
[PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus status) {
    //回主线程弹框
    dispatch_async(dispatch_get_main_queue(), ^{
        if (status == PHAuthorizationStatusDenied  && oldStatus != PHAuthorizationStatusNotDetermined ) { //用户拒绝当前app访问相册
            
        }else if (status == PHAuthorizationStatusAuthorized) {//用户允许当前app访问相册
            [self saveImageIntoAlpbum];
        }else { // 无法访问相册
            [SVProgressHUD showErrorWithStatus:@"因系统原因,无法访问相册!"];
        }
    });
}];
}

pragma mark - 保存图片到相册

 - (void) saveImageIntoAlpbum{
//获得相片
PHFetchResult<PHAsset *> * createdAssets = self.createdAssets;
if (createdAssets == nil) {
    [SVProgressHUD showErrorWithStatus:@"保存图片失败!"];
}

// 获得相册
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:@"保存图片成功!"];
}

//这个方法是异步的
//    [[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{
//        //这个方法必须得嵌套在 [[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{ 内
//
//    } completionHandler:^(BOOL success, NSError * _Nullable error) {
//        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
//            [SVProgressHUD dismiss];
//        });
//    }];
}

相关文章

网友评论

      本文标题:保存图片到自定义胶卷

      本文链接:https://www.haomeiwen.com/subject/wkgeuftx.html