2011-12-31 208 views
1

我想通過UIImagePickerController選擇(或記錄)一個視頻的縮略圖。在我的選取器的FinishedPickingMedia方法中,我得到了我認爲是視頻文件的路徑,並用它將錄製內容保存到相冊中。新錄製的視頻確實出現在我的相冊中。現在得到縮略圖...如何從MonoTouch獲取現有的視頻縮略圖圖像?

我試過在幾種方式中使用ALAssetsLibrary。首先,我列舉了所有視頻,並嘗試從資產的UtiToUrlDictionary.Values [0]值中找到匹配項,從而顯示其文件路徑。沒有條目匹配從FinishedPickingMedia方法的mediaUrlKey獲得的路徑。

我的第二次嘗試是使用ALAssetsLibrary的AssetForUrl()方法。再次,我嘗試了我的FinishedPickingMedia方法的mediaUrlKey,但沒有匹配。

的的UIImagePickerController - > FinishedPickingMedia - > mediaUrlKey返回的路徑:

file://localhost/private/var/mobile/Applications/73037738-C060-4DE6-A1CA-698E6BE083F2/tmp//trim.iRBwWw.MOV 

痛苦的挖掘後,在我的圖書館檢查每一個視頻時,它從ALAssetsLibrary同一個視頻的路徑是:

assets-library://asset/asset.MOV?id=B073FCF6-83ED-436B-AFD8-42D0A6C70FC6&ext=MOV 

爲什麼UIIMagePickerController和ALAssetsLibrary之間的視頻路徑不同?我怎樣才能讓ALAssetsLibrary返回從UIImagePickerController中選取的相同視頻?

[爲選擇器代碼]

public override void FinishedPickingMedia (UIImagePickerController picker, NSDictionary info) 
{ 
    var mediaUrlKey = new NSString("UIImagePickerControllerMediaURL"); 
    var mediaPath = (NSUrl) info.ObjectForKey(mediaUrlKey); 


    if(recordView.RecordVideo) 
    { 
     if(mediaPath != null) 
     { 
      if(UIVideo.IsCompatibleWithSavedPhotosAlbum(mediaPath.Path)) 
      { 
       UIVideo.SaveToPhotosAlbum(mediaPath.Path, SaveToPhotosAlbumResults); 
      } 
      else 
      { 
       using (var alert = new UIAlertView("Problem Encountered", 
       "Unable to save this recording.", null, "Ok!", null)) 
       { 
        alert.Show(); 
       } 
      } 
     } 
    } 
    else 
    { 
     recordView.VideoFileLocation = mediaPath; 
     //ALAssetsLibrary library = new ALAssetsLibrary(); 
     //library.AssetForUrl(mediaPath, GetAssetResult, GetAssetError); 
    } 

    recordView.VideoFileLocation = mediaPath; 

    Console.WriteLine("***" + recordView.VideoFileLocation.ToString()+ "***"); 

    recordView.ShowUploadButton(); 
    recordView.SetThumbnailImage(); 

    picker.DismissModalViewControllerAnimated(true); 
} 

謝謝

回答

0

我認爲部分Can I Get To The Original File Representing This Image on the Diskin this question可以幫助你。

0

這是你如何能得到機械臂代表中的縮略圖:

NSUrl data = new NSUrl(info.ObjectForKey(new NSString("UIImagePickerControllerMediaURL")).ToString()); 

       //get the video thumbnail 

       MPMoviePlayerController movie = new MPMoviePlayerController(data); 
       movie.Stop(); 
       UIImage videoThumbnail = movie.ThumbnailImageAt(0.0,MPMovieTimeOption.NearestKeyFrame); 
       movie.Stop(); 
1

我覺得可能爲時已晚,但下面的代碼工作,截至目前

public static UIImage GenerateImage(NSUrl videoUrl) 
{ 
    var asset = AVAsset.FromUrl(videoUrl); 
    var imageGenerator = AVAssetImageGenerator.FromAsset(asset); 
    imageGenerator.AppliesPreferredTrackTransform = true; 

    var actualTime = asset.Duration; 
    CoreMedia.CMTime cmTime = new CoreMedia.CMTime(1, 60); 

    NSError error; 
    var imageRef = imageGenerator.CopyCGImageAtTime(cmTime, out actualTime, out error); 

    if (imageRef == null) 
     return null; 

    var image = UIImage.FromImage(imageRef); 

    return image; 
} 
相關問題