29,027
社区成员
发帖
与我相关
我的任务
分享
#pragma mark - Private Method
-(void)_addAssetURL:(NSURL *)assetURL
toAlbum:(NSString *)albumName
failureBlock:(ALAssetsLibraryAccessFailureBlock)failureBlock {
__block BOOL albumWasFound = NO;
ALAssetsLibraryGroupsEnumerationResultsBlock enumerationBlock;
enumerationBlock = ^(ALAssetsGroup *group, BOOL *stop) {
// compare the names of the albums
if ([albumName compare:[group valueForProperty:ALAssetsGroupPropertyName]] == NSOrderedSame) {
// target album is found
albumWasFound = YES;
// get a hold of the photo's asset instance
[self assetForURL:assetURL
resultBlock:^(ALAsset *asset) {
// add photo to the target album
[group addAsset:asset];
}
failureBlock:failureBlock];
// album was found, bail out of the method
return;
}
if (group == nil && albumWasFound == NO) {
// photo albums are over, target album does not exist, thus create it
// Since you use the assets library inside the block,
// ARC will complain on compile time that there’s a retain cycle.
// When you have this – you just make a weak copy of your object.
//
// __weak ALAssetsLibrary * weakSelf = self;
//
// by @Marin.
//
// I don't use ARC right now, and it leads a warning.
// by @Kjuly
ALAssetsLibrary * weakSelf = self;
// create new assets album
[self addAssetsGroupAlbumWithName:albumName
resultBlock:^(ALAssetsGroup *group) {
// get the photo's instance
[weakSelf assetForURL:assetURL
resultBlock:^(ALAsset *asset) {
// add photo to the newly created album
[group addAsset:asset];
}
failureBlock:failureBlock];
}
failureBlock:failureBlock];
// should be the last iteration anyway, but just in case
return;
}
};
// search all photo albums in the library
[self enumerateGroupsWithTypes:ALAssetsGroupAlbum
usingBlock:enumerationBlock
failureBlock:failureBlock];
}
typedef void(^enumerationBlock) (ALAssetsGroup *group, BOOL *stop)
其中的参数要根据你的需要定义,如果你想将哪些数据回传,你就定义哪些数据。如上这个block.它是将 ALAssetsGroup 和 布尔类型的stop回传