ALAssetLibrary可以在Document文件夹中加载图像文件吗?

时间:2015-03-13 06:32:09

标签: ios metadata alassetslibrary

ALAssetLibrary可以非常轻松地从相机胶卷或相册中获取图像文件信息。但是,当我尝试使用它来加载Document文件夹(沙盒)中的图像文件时,它总是一无所获。这是我的代码:

//Get full file path in the Document folder.(Yes, IMG001.jpg exists in that place)
NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString* theFileName = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"IMG001.jpg"];
NSURL* theFileURL = [NSURL fileURLWithPath:theFileName];

//Now try to load the asset.
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
[library assetForURL:theFileURL resultBlock:^(ALAsset *asset) {
    //asset here is always nil.
} failureBlock:^(NSError *error) {
    //it does not run in this place.
}];

有人可以告诉我为什么吗?

1 个答案:

答案 0 :(得分:0)

传递给 assetForURL 函数的网址应该是先前从 ALAsset 对象检索到的资产网址。 ALAsset 对象代表由照片应用管理的照片或视频。所以在这种情况下你不能使用ALAssetLibrary。

要从文档目录中获取图像,请使用以下命令:

    NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString* imageFilePath = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"IMG001.jpg"];
    UIImage* imageRequired = [UIImage imageWithContentsOfFile: imageFilePath];