AssetForURL returns nil

I am trying to use the AssetForURL method, but it returns zero.

This is the code I'm using:

-(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 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 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 I give:

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

I get the URL from the camera delegation method:

 -(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]); } }]; } 

Any ideas?

+6
source share
1 answer

Do you have access to the asset library? Remember to check access status

 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 

Description Invokes a given block passing as a parameter, each of the group of assets corresponding to the type of group of assets. The results are passed one by one to the caller, executing an enumeration block. This method is asynchronous. When the groups are listed, the user may be asked to confirm the access of the application to the data; the method, though, returns immediately. You must do whatever work you want with the assets in the Block enumeration.

If the user denies access to the application, or if no application is allowed access to the data, failBlock is called.

0
source

Source: https://habr.com/ru/post/943870/


All Articles