我分叉了这个很棒的 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,正在寻求帮助。
谢谢