适用于Titanium的ALAsset模块可以在模拟器上运行,但会在设备上崩溃任何提示?

时间:2012-05-23 00:47:07

标签: titanium-modules

我分叉了这个伟大的Titanium模块来访问ALAsset。 https://github.com/amigoni/titanium-mobile-alasset-module

我正在尝试访问图片数据和信息。它似乎在模拟器中工作正常,但在设备上却没有。

这是我怀疑代码

的地方
-(void)assetThumbnails:(id)args
{
ENSURE_UI_THREAD_1_ARG(args);
ENSURE_SINGLE_ARG(args,NSDictionary);

id onthumb       = [args objectForKey:@"thumbnailCallback"];
NSString *group = [args objectForKey:@"group"];
ENSURE_STRING_OR_NIL(group);
int page        = [TiUtils intValue:[args objectForKey:@"page"] def:1] - 1;
int perPage     = [TiUtils intValue:[args objectForKey:@"perPage"] def:25];

int fromIndex   = page * perPage;
int toIndex     = fromIndex + perPage;

NSUInteger groupTypes = ALAssetsGroupSavedPhotos;
if( group == nil ){
    group = @"savedPhotos";
}
if( [group isEqualToString:@"savedPhotos"] ){
    groupTypes  = ALAssetsGroupSavedPhotos;
} else if( [group isEqualToString:@"photoStream"] ){
    groupTypes  = ALAssetsGroupPhotoStream;
} else if( [group isEqualToString:@"faces"] ){
    groupTypes  = ALAssetsGroupFaces;
} else if( [group isEqualToString:@"all"] ){
    groupTypes  = ALAssetsGroupAll;
}
NSMutableArray *assets  = [[NSMutableArray alloc] init];
RELEASE_TO_NIL(thumbCallback);
thumbCallback  = [onthumb retain];
void (^assetGroupEnumerator) (ALAssetsGroup *, BOOL *) = ^(ALAssetsGroup *group, BOOL *stop){
    if(group != nil) {
        [group enumerateAssetsUsingBlock:^(ALAsset *result, NSUInteger index, BOOL *stop) {
            if( result != nil )
            {
                if( (index >= fromIndex) && (index <= toIndex) ){
                    NSURL *url = [[result defaultRepresentation] url];
                    NSString *sUrl = [url absoluteString];
                    NSDictionary *metadata = result.defaultRepresentation.metadata; 
                        UIImage *thumbnail = [UIImage imageWithCGImage:[result thumbnail]];
                        NSDictionary *event = [NSDictionary 
                                               dictionaryWithObjectsAndKeys:
                                               [[[TiBlob alloc] initWithImage:thumbnail] autorelease],
                                               @"thumbnail",
                                               NUMINT(index),
                                               @"index",
                                               sUrl,
                                               @"url",
                                               metadata,
                                               @"metadata",
                                               nil];
                        [assets addObject:event];
                }
            } else {
                if (thumbCallback!=nil)
                {
                    NSDictionary *event = [NSDictionary dictionaryWithObjectsAndKeys:assets,@"assets", nil];
                    [self _fireEventToListener:@"onThumbnail" withObject:event listener:thumbCallback thisObject:nil];
                    [assets release];
                }
            }
        }];
    }
};

ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];

[library enumerateGroupsWithTypes:groupTypes
                       usingBlock:assetGroupEnumerator
                     failureBlock:^(NSError *error) {}];
[library release];
}

有趣的是,代码似乎在第26项之后崩溃。您从上面的代码中看到的将是第二页的第一页。

我真的不太了解Objective C并且正在寻求一些帮助。

由于

1 个答案:

答案 0 :(得分:0)

也许[发布资产]会引发异常。我的情况就是这样。

在分配资产时,[[[NSMutableArray alloc] init] autorelease]并删除该行; 例外情况不会提出。