@implementation
ALAssetsLibrary
(ZHExpand)
- (
void
)zh_createAssetsGroupWithName:(NSString*)name
enumerateGroupsFailureBlock
:(
void
(^) (
NSError
*error))enumerateGroupsFailureBlock
hasTheNewGroupBlock
:(
void
(^) (
ALAssetsGroup
*group))hasGroup
createSuccessedBlock
:(
void
(^) (
ALAssetsGroup
*group))createSuccessedBlock
createFaieldBlock
:(
void
(^) (
NSError
*error))createFaieldBlock
{
__block
BOOL
hasTheNewGroup =
NO
;
[
self
enumerateGroupsWithTypes
:
ALAssetsGroupAlbum
usingBlock
:^(
ALAssetsGroup
*group,
BOOL
*stop) {
hasTheNewGroup = [name
isEqualToString
:[group
valueForProperty
:ALAssetsGroupPropertyName]];
if
(hasTheNewGroup) {
[
ZHBlock
dispatchOnMainThread
:^{
hasGroup(group);
}];
*stop =
YES
;
}
if
(!group && !hasTheNewGroup && !*stop) {
[
self
addAssetsGroupAlbumWithName
:name
resultBlock
:^(
ALAssetsGroup
*agroup) {
[
ZHBlock
dispatchOnMainThread
:^{
createSuccessedBlock(agroup);
}];
}
failureBlock
:^(
NSError
*error) {
[
ZHBlock
dispatchOnMainThread
:^{
createFaieldBlock(error);
}];
}];
}
}
failureBlock
:^(
NSError
*error) {
[
ZHBlock
dispatchOnMainThread
:^{
enumerateGroupsFailureBlock(error);
}];
}];
}
- (
void
)zh_saveVideoWithVideoPath:(NSString*)path
toAlbum
:(NSString*)name
saveSuccessBlock
:(
void
(^) (
ALAssetsGroup
*group,
NSURL
*assetURL,
ALAsset
*asset))saveSuccessBlock
saveFaieldBlock
:(
void
(^) (
NSError
*error))saveFaieldBlock
{
[
self
writeVideoAtPathToSavedPhotosAlbum
:[
NSURL
fileURLWithPath
:path]
completionBlock
:^(
NSURL
*assetURL,
NSError
*error) {
if
(error) {
[
ZHBlock
dispatchOnMainThread
:^{
saveFaieldBlock(error);
}];
return
;
}
[
self
zh_addVideoToAssetGroupWithAssetUrl
:assetURL
toAlbum
:name
addSuccessBlock
:^(
ALAssetsGroup
*targetGroup,
NSURL
*currentAssetUrl,
ALAsset
*currentAsset) {
[
ZHBlock
dispatchOnMainThread
:^{
saveSuccessBlock(targetGroup,currentAssetUrl,currentAsset);
}];
}
addFaieldBlock
:^(
NSError
*error) {
[
ZHBlock
dispatchOnMainThread
:^{
saveFaieldBlock(error);
}];
}];
}];
}
- (
void
)zh_addVideoToAssetGroupWithAssetUrl:(NSURL*)assetURL
toAlbum
:(NSString*)name
addSuccessBlock
:(
void
(^) (
ALAssetsGroup
*targetGroup,
NSURL
*currentAssetUrl,
ALAsset
*currentAsset))addSuccessBlock
addFaieldBlock
:(
void
(^) (
NSError
*error))addFaieldBlock
{
[
self
zh_createAssetsGroupWithName
:name
enumerateGroupsFailureBlock
:^(
NSError
*error) {
if
(error) {
[
ZHBlock
dispatchOnMainThread
:^{
addFaieldBlock(error);
}];
return
;
}
}
hasTheNewGroupBlock
:^(
ALAssetsGroup
*group) {
[
self
assetForURL
:assetURL
resultBlock
:^(
ALAsset
*asset) {
[group
addAsset
:asset];
[
ZHBlock
dispatchOnMainThread
:^{
addSuccessBlock(group,assetURL,asset);
}];
}
failureBlock
:^(
NSError
*error) {
if
(error) {
[
ZHBlock
dispatchOnMainThread
:^{
addFaieldBlock(error);
}];
return
;
}
}];
}
createSuccessedBlock
:^(
ALAssetsGroup
*group) {
[
self
assetForURL
:assetURL
resultBlock
:^(
ALAsset
*asset) {
[group
addAsset
:asset];
[
ZHBlock
dispatchOnMainThread
:^{
addSuccessBlock(group,assetURL,asset);
}];
}
failureBlock
:^(
NSError
*error) {
if
(error) {
[
ZHBlock
dispatchOnMainThread
:^{
addFaieldBlock(error);
}];
return
;
}
}];
}
createFaieldBlock
:^(
NSError
*error) {
if
(error) {
[
ZHBlock
dispatchOnMainThread
:^{
addFaieldBlock(error);
}];
return
;
}
}];
}