2013-04-29 86 views
6

我試圖使用AssetForURL方法,但它返回零。AssetForURL返回零

這是我使用的代碼:

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

} 

我給它的網址是:

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

我從相機的委託方法得到的網址:

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

} 

任何想法?

+0

您確定您可以訪問網址嗎?在設備/模擬器的瀏覽器中嘗試。 – Alexander 2013-04-29 10:21:58

+0

'assetForURL'沒有返回類型(void)。你的意思是resultBlock中的資產是零嗎? – Felix 2013-04-29 10:26:16

+0

是的,這就是我的意思 – user2328703 2013-04-29 10:42:43

回答

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 

說明調用給定塊作爲參數傳遞的每個匹配給定的資產組類型的 資產組。結果是 通過執行枚舉塊逐個傳遞給調用者。 此方法是異步的。當枚舉組時,用戶可能會要求 確認應用程序訪問數據;儘管如此,方法 立即返回。您應該使用enumerationBlock中的資產執行您想要的任何工作 。

如果用戶拒絕訪問 應用程序,或者如果沒有應用程序被允許訪問數據,則會調用 failureBlock。