[英]break iteration of photos ALAssetsLibrary
How do I break through the enumeration going on for ALAssetsLibrary enumerateAssets method with a boolean set. 如何使用布尔集突破ALAssetsLibrary enumerateAssets方法的枚举。 Can I just get out of the loop?
我可以摆脱循环吗?
CODE: 码:
[self.library enumerateGroupsWithTypes:ALAssetsGroupSavedPhotos usingBlock:^(ALAssetsGroup *group, BOOL *stop) {
@try {
if(group != nil) {
@autoreleasepool {
int newNumberOfPhotos = [group numberOfAssets];
if (self.numberOfPhotosInSavedPhotos < newNumberOfPhotos) {
//only new photos
NSRange range = NSMakeRange(self.numberOfPhotosInSavedPhotos, newNumberOfPhotos-self.numberOfPhotosInSavedPhotos);
NSIndexSet *indexSet = [NSIndexSet indexSetWithIndexesInRange:range];
[group enumerateAssetsAtIndexes:indexSet options:0 usingBlock:^(ALAsset *result, NSUInteger index, BOOL *stop) {
@autoreleasepool {
if(someCondition) {
//get out of the enumeration block (that is, exit the method) or go to complete block
}
NSString *assetType = [result valueForProperty:ALAssetPropertyType];
}
} ];
}
}
} else {
//enumeration ended
}
}
@catch (NSException *e) {
NSLog(@"exception streaming: %@", [e description]);
}
}failureBlock:^(NSError *error){
NSLog(@"Error retrieving albums stream: %@", [error description]);
if (error.code==-3312 || error.code==-3311) {
}
}];
To stop the assets enumeration, just set *stop = YES
in the enumeration block. 要停止资产枚举,只需在枚举块中设置
*stop = YES
。
If you want to stop both the outer and the inner enumeration, use different names for the stop variable and set both to YES
: 如果要同时停止外部枚举和内部枚举,请对stop变量使用不同的名称,并将两者都设置为
YES
:
[self.library enumerateGroupsWithTypes:ALAssetsGroupSavedPhotos usingBlock:^(ALAssetsGroup *group, BOOL *outerStop) {
...
[group enumerateAssetsAtIndexes:indexSet options:0 usingBlock:^(ALAsset *result, NSUInteger index, BOOL *innerStop) {
if (someCondition) {
*innerStop = YES;
*outerStop = YES;
} else {
// process asset
}
}
}
Remarks: The @try/@catch
block should normally not be necessary if you don't have a programming error inside your loops. 备注:如果循环中没有编程错误,通常不需要
@try/@catch
块。
Your check for "new photos" looks suspicious, because the number of assets in each group is compared with the same number self.numberOfPhotosInSavedPhotos
, perhaps you should check that part again. 您对“新照片”的检查看起来很可疑,因为每组中的资产数量与相同的数字
self.numberOfPhotosInSavedPhotos
,也许您应该再次检查该部分。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.