私のアプリでは、ユーザーは写真を撮って特定のタスクに関連付けることができます。私のアプローチは、このcategoryを使用してカスタム名のALAssetsGroupを作成し、その中に写真を保存することでした。問題は、それらの写真をすべて取り込んでサムネイルとして表示するときに発生します。私はこのような資産をつかんだ:ALAssetsLibraryを列挙する
2012-04-02 10:05:26.585 MyApp[2746:7d3b] *** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[NSOrderedSet enumerateObjectsAtIndexes:options:usingBlock:]: index 0 beyond bounds for empty ordered set'
例外がenumerateAssetsUsingBlockでスロー:メソッド
- (void)setImagesForTask:(Task *)task {
//clear images associated with the task so we don't double up.
[task clearImages];
//static instance of ALAssetsLibrary
lib = [JobStore defaultAssetsLibrary];
dispatch_queue_t queue = dispatch_get_main_queue();
dispatch_async(queue, ^(void){
[lib enumerateGroupsWithTypes:ALAssetsGroupAlbum
usingBlock:^(ALAssetsGroup *group, BOOL *stop) {
NSLog(@"group:%@", group);
//find my custom photo album and if it's there, enumerate through it.
if (group != nil && [[group valueForProperty:ALAssetsGroupPropertyName] isEqualToString:
[NSString stringWithFormat:@"Media for %@", task.title]]) {
if (group.numberOfAssets != 0) {
[group enumerateAssetsUsingBlock:^(ALAsset *result, NSUInteger index, BOOL *stop) {
if (result != nil) {
CGImageRef imgRef = [[result defaultRepresentation] fullScreenImage];
UIImage *img = [UIImage imageWithCGImage:imgRef];
//methods to resize the image and get PNG representations…
[task setThumbnailDataFromImage:img];
[task setLargeImageDataFromImage:img];
NSLog(@"saved image to: %@", group);
} else if (result == nil){
NSLog(@"enumeration of assets ended");
}
}];
}
//if the group isn't there...
} else if (group == nil) {
//this method removes a UIView with a UIActivityIndicatorView that appears while enumeration occurs.
[self performSelectorOnMainThread:@selector(removeView)
withObject:nil
waitUntilDone:NO];
NSLog(@"enumeration of groups ended");
}}
failureBlock:^(NSError *error) {
NSLog(@"FAILURE");
}];
});
JobStore *js = [JobStore defaultStore];
[js saveChanges];
}
これは私が得る例外があります。これは特に、あるタスクに写真を追加し、別のタスクに移動してその写真に写真を追加すると起こりがちです。
2012-04-02 10:05:26.580 MyApp[2746:707] group:ALAssetsGroup - Name:Media for (current task), Type:Album, Assets count:1
だから資産グループが実際には空ではありませんが、私はその中に資産を列挙しようとすると、私のアプリは思わ: 一つ奇妙なことは、例外がスローされる前に、私は私のログでこれを持っている、ということですそこには何もないと思う。 おそらく、これらの列挙型がネストされているか、またはブロックがバックグラウンドで実行されているが、メソッドがすぐに戻り、アセットが何かを含むことがわかる前にアセットグループを列挙しようとしているからです。これには最善の方法がありますか?私は、これらのメソッドのブロックがどのように実行されるかを理解していないという基本的なことがあると思います。どんな入力も非常に高く評価されます。
ありがとう:
は、私はここに掲載この範囲の例外を解決する方法を発見しました。私はそれを解決したと信じて、問題は私にとっては悪い設計決定でした。私は今日答えを掲示しますが、十分な担当者がいないためにまだできません。 – geraldWilliam