保存视频到自定义相册
1 @implementation ALAssetsLibrary (ZHExpand) 2 3 /** 4 * 创建本地相册 5 * 6 * @param name 相册名称 7 * @param enumerateGroupsFailureBlock 遍历相册分组失败回调 8 * @param hasGroup 本地已经存在该相册,请重新命名 9 * @param createSuccessedBlock 创建相册成功回调 10 * @param createFaieldBlock 创建相册失败回调 11 */ 12 - (void)zh_createAssetsGroupWithName:(NSString*)name 13 enumerateGroupsFailureBlock:(void (^) (NSError *error))enumerateGroupsFailureBlock 14 hasTheNewGroupBlock:(void (^) (ALAssetsGroup *group))hasGroup 15 createSuccessedBlock:(void (^) (ALAssetsGroup *group))createSuccessedBlock 16 createFaieldBlock:(void (^) (NSError *error))createFaieldBlock 17 { 18 19 __block BOOL hasTheNewGroup = NO; 20 21 [self enumerateGroupsWithTypes:ALAssetsGroupAlbum usingBlock:^(ALAssetsGroup *group, BOOL *stop) { 22 23 hasTheNewGroup = [name isEqualToString:[group valueForProperty:ALAssetsGroupPropertyName]]; 24 if (hasTheNewGroup) { 25 26 [ZHBlock dispatchOnMainThread:^{ 27 28 hasGroup(group); 29 }]; 30 *stop = YES; 31 } 32 if (!group && !hasTheNewGroup && !*stop) {//遍历完毕,本地没有该文件夹,非手动停止的遍历 33 34 [self addAssetsGroupAlbumWithName:name resultBlock:^(ALAssetsGroup *agroup) { 35 36 [ZHBlock dispatchOnMainThread:^{ 37 38 createSuccessedBlock(agroup); 39 }]; 40 } failureBlock:^(NSError *error) { 41 42 [ZHBlock dispatchOnMainThread:^{ 43 44 createFaieldBlock(error); 45 }]; 46 }]; 47 } 48 } failureBlock:^(NSError *error) { 49 50 [ZHBlock dispatchOnMainThread:^{ 51 52 enumerateGroupsFailureBlock(error); 53 }]; 54 }]; 55 } 56 57 /** 58 * 保存视频到指定相册(直接调用可保存到指定分组) 59 * 60 * @param path 视频路径 61 * @param name 相册名称 62 * @param saveSuccessBlock 保存成功回调 63 * @param saveFaieldBlock 保存失败回调 64 */ 65 - (void)zh_saveVideoWithVideoPath:(NSString*)path 66 toAlbum:(NSString*)name 67 saveSuccessBlock:(void (^) (ALAssetsGroup *group, NSURL *assetURL, ALAsset *asset))saveSuccessBlock 68 saveFaieldBlock:(void (^) (NSError *error))saveFaieldBlock 69 { 70 71 [self writeVideoAtPathToSavedPhotosAlbum:[NSURL fileURLWithPath:path] completionBlock:^(NSURL *assetURL, NSError *error) {//先添加到公众相册 72 73 if (error) { 74 [ZHBlock dispatchOnMainThread:^{ 75 76 saveFaieldBlock(error); 77 }]; 78 return; 79 } 80 81 [self zh_addVideoToAssetGroupWithAssetUrl:assetURL toAlbum:name addSuccessBlock:^(ALAssetsGroup *targetGroup, NSURL *currentAssetUrl, ALAsset *currentAsset) { 82 83 [ZHBlock dispatchOnMainThread:^{ 84 85 saveSuccessBlock(targetGroup,currentAssetUrl,currentAsset); 86 }]; 87 } addFaieldBlock:^(NSError *error) { 88 89 [ZHBlock dispatchOnMainThread:^{ 90 91 saveFaieldBlock(error); 92 }]; 93 }]; 94 }]; 95 } 96 97 /** 98 * 添加相册视频到指定分组 99 * 100 * @param assetURL 视频在相册的URL 101 * @param name 指定分组名称 102 * @param addSuccessBlock 添加成功回调 103 * @param addFaieldBlock 添加失败回调 104 */ 105 - (void)zh_addVideoToAssetGroupWithAssetUrl:(NSURL*)assetURL 106 toAlbum:(NSString*)name 107 addSuccessBlock:(void (^) (ALAssetsGroup *targetGroup, NSURL *currentAssetUrl, ALAsset *currentAsset))addSuccessBlock 108 addFaieldBlock:(void (^) (NSError *error))addFaieldBlock 109 { 110 111 [self zh_createAssetsGroupWithName:name enumerateGroupsFailureBlock:^(NSError *error) { 112 113 if (error) { 114 [ZHBlock dispatchOnMainThread:^{ 115 116 addFaieldBlock(error); 117 }]; 118 return ; 119 } 120 } hasTheNewGroupBlock:^(ALAssetsGroup *group) { 121 122 [self assetForURL:assetURL resultBlock:^(ALAsset *asset) {//得到视频的ALAsset实例 123 124 [group addAsset:asset];//添加视频到指定相册分组 125 [ZHBlock dispatchOnMainThread:^{ 126 127 addSuccessBlock(group,assetURL,asset); 128 }]; 129 } failureBlock:^(NSError *error) { 130 131 if (error) { 132 [ZHBlock dispatchOnMainThread:^{ 133 134 addFaieldBlock(error); 135 }]; 136 return ; 137 } 138 }]; 139 } createSuccessedBlock:^(ALAssetsGroup *group) { 140 141 [self assetForURL:assetURL resultBlock:^(ALAsset *asset) { 142 143 [group addAsset:asset]; 144 [ZHBlock dispatchOnMainThread:^{ 145 146 addSuccessBlock(group,assetURL,asset); 147 }]; 148 } failureBlock:^(NSError *error) { 149 150 if (error) { 151 [ZHBlock dispatchOnMainThread:^{ 152 153 addFaieldBlock(error); 154 }]; 155 return ; 156 } 157 }]; 158 } createFaieldBlock:^(NSError *error) { 159 160 if (error) { 161 [ZHBlock dispatchOnMainThread:^{ 162 163 addFaieldBlock(error); 164 }]; 165 return ; 166 } 167 }]; 168 }