AssetForURL 返回 nil。

6
我正在尝试使用AssetForURL方法,但它返回了nil。
这是我正在使用的代码:
-(void)addAssetURL:(NSURL*)assetURL toAlbum:(NSString*)albumName withCompletionBlock:(SaveImageCompletion)completionBlock
{
__block BOOL albumWasFound = NO;

//search all photo albums in the library
[self enumerateGroupsWithTypes:ALAssetsGroupAlbum 
                    usingBlock:^(ALAssetsGroup *group, BOOL *stop) {

                        //compare the names of the albums
                        if ([albumName compare: [group valueForProperty:ALAssetsGroupPropertyName]]==NSOrderedSame) {

                            //target album is found
                            albumWasFound = YES;

                            //get a hold of the photo's asset instance
                            [self assetForURL: assetURL
                                  resultBlock:^(ALAsset *asset) {
                                      //add photo to the target album
                                      [group addAsset: asset];
                                      //run the completion block
                                      completionBlock(nil);

                                  } failureBlock: completionBlock];

                            //album was found, bail out of the method
                            return;
                        }

                        if (group==nil && albumWasFound==NO) {
                            //photo albums are over, target album does not exist, thus create it

                            __weak ALAssetsLibrary* weakSelf = self;

                            //create new assets album
                            [self addAssetsGroupAlbumWithName:albumName 
                                                  resultBlock:^(ALAssetsGroup *group) {

                                                      //get the photo's instance
                                                      [weakSelf assetForURL: assetURL 
                                                                    resultBlock:^(ALAsset *asset) {

                                                                        //add photo to the newly created album
                                                                        [group addAsset: asset];

                                                                        //call the completion block
                                                                        completionBlock(nil);

                                                                    } failureBlock: completionBlock];

                                                  } failureBlock: completionBlock];

                            //should be the last iteration anyway, but just in case
                            return;
                        }

                    } failureBlock: completionBlock];

}

我提供的 URL 是:
    file://localhost/private/var/mobile/Applications/6630FBD3-1212-4ED0-BC3B-0C23AEEFB267/tmp/capture-T0x1d56e310.tmp.N3SZXy/capturedvideo.MOV

我从相机委托方法中获取URL:

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {

    NSLog(@"%@",[info objectForKey:UIImagePickerControllerMediaURL]);

    [library addAssetURL:[info objectForKey:UIImagePickerControllerMediaURL] toAlbum:@"Compedia videos" withCompletionBlock:^(NSError *error) {
        if (error!=nil) {
            NSLog(@"Big error: %@", [error description]);
        }
    }];

}

有什么想法吗?

你确认你能够访问URL吗?在设备或模拟器的浏览器中尝试一下。 - Alexander
assetForURL 没有返回类型(void)。您是指 resultBlock 中的 asset 为 nil 吗? - Felix
是的,这就是我想表达的意思。 - user2328703
我遇到了完全相同的问题,@user2328703,你找到解决方案了吗? - SamChen
1个回答

0

你确定你有访问资产库的权限吗? 别忘了检查访问状态

ALAuthorizationStatus status = [ALAssetsLibrary authorizationStatus];
if (status == ALAuthorizationStatusNotDetermined) {
    ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
    [ALAssetsLibrary authorizationStatus];
    __block BOOL accessChecked = NO; /// *stop is not respected immediately
    [library enumerateGroupsWithTypes:ALAssetsGroupAll usingBlock:^(ALAssetsGroup *group, BOOL *stop) {
        if (accessChecked) return ;
        *stop = YES;
        accessChecked = YES;
    } failureBlock:^(NSError *error){
    }];
}
else {
    BOOL granted = status == ALAuthorizationStatusAuthorized;
}

- (void)enumerateGroupsWithTypes:(ALAssetsGroupType)types usingBlock:(ALAssetsLibraryGroupsEnumerationResultsBlock)enumerationBlock failureBlock:(ALAssetsLibraryAccessFailureBlock)failureBlock

描述 通过调用给定的块并将匹配给定资产组类型的每个资产组作为参数传递,来调用。结果通过执行枚举块逐个传递给调用者。此方法是异步的。当枚举组时,用户可能会被要求确认应用程序对数据的访问;但是,该方法立即返回。您应该在枚举块中执行所需的工作。
如果用户拒绝访问应用程序,或者不允许任何应用程序访问数据,则调用failureBlock。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接