美文网首页iOS自我学习库
iOS 保存照片和视频进入到用户创建的相册中

iOS 保存照片和视频进入到用户创建的相册中

作者: levinYuXiao | 来源:发表于2017-02-21 20:09 被阅读1457次

创建相册

- (BOOL)isExistFolder:(NSString *)folderName {
    //首先获取用户手动创建相册的集合
    PHFetchResult *collectonResuts = [PHCollectionList fetchTopLevelUserCollectionsWithOptions:nil];
    
    __block BOOL isExisted = NO;
    //对获取到集合进行遍历
    [collectonResuts enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
        PHAssetCollection *assetCollection = obj;
        //folderName是我们写入照片的相册
        if ([assetCollection.localizedTitle isEqualToString:folderName])  {
            isExisted = YES;
        }
    }];
    
    return isExisted;
}

- (void)createFolder:(NSString *)folderName {
    if (![self isExistFolder:folderName]) {
        [[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{
            //添加HUD文件夹
            [PHAssetCollectionChangeRequest creationRequestForAssetCollectionWithTitle:folderName];
            
        } completionHandler:^(BOOL success, NSError * _Nullable error) {
            if (success) {
                NSLog(@"创建相册文件夹成功!");
            } else {
                NSLog(@"创建相册文件夹失败:%@", error);
            }
        }];
    }
}

保存照片


NSURL *url = [NSURL fileURLWithPath:imagePath];
    
//标识保存到系统相册中的标识
__block NSString *localIdentifier;

//首先获取相册的集合
PHFetchResult *collectonResuts = [PHCollectionList fetchTopLevelUserCollectionsWithOptions:nil];
//对获取到集合进行遍历
[collectonResuts enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
    PHAssetCollection *assetCollection = obj;
    //Camera Roll是我们写入照片的相册
    if ([assetCollection.localizedTitle isEqualToString:_folderName])  {
        [[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{
            //请求创建一个Asset
            PHAssetChangeRequest *assetRequest = [PHAssetChangeRequest creationRequestForAssetFromImageAtFileURL:url];
            //请求编辑相册
            PHAssetCollectionChangeRequest *collectonRequest = [PHAssetCollectionChangeRequest changeRequestForAssetCollection:assetCollection];
            //为Asset创建一个占位符,放到相册编辑请求中
            PHObjectPlaceholder *placeHolder = [assetRequest placeholderForCreatedAsset];
            //相册中添加照片
            [collectonRequest addAssets:@[placeHolder]];
            
            localIdentifier = placeHolder.localIdentifier;
        } completionHandler:^(BOOL success, NSError *error) {
            if (success) {
                NSLog(@"保存图片成功!");
                NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithDictionary:[self readFromPlist]];
                [dict setObject:localIdentifier forKey:[self showFileNameFromPath:imagePath]];
                [self writeDicToPlist:dict];
            } else {
                NSLog(@"保存图片失败:%@", error);
            }
        }];
    }
}];

保存视频


NSURL *url = [NSURL fileURLWithPath:videoPath];
    
//标识保存到系统相册中的标识
__block NSString *localIdentifier;

//首先获取相册的集合
PHFetchResult *collectonResuts = [PHCollectionList fetchTopLevelUserCollectionsWithOptions:nil];
//对获取到集合进行遍历
[collectonResuts enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
    PHAssetCollection *assetCollection = obj;
    //folderName是我们写入照片的相册
    if ([assetCollection.localizedTitle isEqualToString:_folderName])  {
        [[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{
            //请求创建一个Asset
            PHAssetChangeRequest *assetRequest = [PHAssetChangeRequest creationRequestForAssetFromVideoAtFileURL:url];
            //请求编辑相册
            PHAssetCollectionChangeRequest *collectonRequest = [PHAssetCollectionChangeRequest changeRequestForAssetCollection:assetCollection];
            //为Asset创建一个占位符,放到相册编辑请求中
            PHObjectPlaceholder *placeHolder = [assetRequest placeholderForCreatedAsset];
            //相册中添加视频
            [collectonRequest addAssets:@[placeHolder]];
            
            localIdentifier = placeHolder.localIdentifier;
        } completionHandler:^(BOOL success, NSError *error) {
            if (success) {
                NSLog(@"保存视频成功!");
                NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithDictionary:[self readFromPlist]];
                [dict setObject:localIdentifier forKey:[self showFileNameFromPath:videoPath]];
                [self writeDicToPlist:dict];
            } else {
                NSLog(@"保存视频失败:%@", error);
            }
        }];
    }
}];

相关文章

网友评论

    本文标题:iOS 保存照片和视频进入到用户创建的相册中

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