PHAssetChangeRequest可可错误-1

时间:2018-02-27 15:15:18

标签: c# ios xamarin xamarin.ios

我需要将下载的视频保存到iPhone上的图库,但收到错误:

The operation couldnt be completed. (Cocoa error -1/)

还尝试通过webClient.DownloadDataAsync()执行此操作,获得相同的错误。这是我的清单:

public async Task<string> DownloadFile(string fileUri)
        {
     var tcs = new TaskCompletionSource<string>();
            string fileName = fileUri.Split('/').Last();
            var documentsDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
            string videoFileName = System.IO.Path.Combine(documentsDirectory, fileName);
            var webClient = new WebClient();
webClient.DownloadFileCompleted += (s, e) =>
            {
                 var authStatus = await PHPhotoLibrary.RequestAuthorizationAsync();
                if(authStatus == PHAuthorizationStatus.Authorized){
                    var fetchOptions = new PHFetchOptions();
                    var collections = PHAssetCollection.FetchAssetCollections(PHAssetCollectionType.Album, PHAssetCollectionSubtype.Any, fetchOptions);
                    collection = collections.firstObject as PHAssetCollection;

                    PHPhotoLibrary.SharedPhotoLibrary.PerformChanges(() => {
                        var assetCreationRequest = PHAssetChangeRequest.FromVideo(NSUrl.FromFileName(videoFileName));
                        var assetPlaceholder = assetCreationRequest.PlaceholderForCreatedAsset;
                        var albumChangeRequest = PHAssetCollectionChangeRequest.ChangeRequest(collection);
                        albumChangeRequest.AddAssets(new PHObject[] { assetPlaceholder });
                    }, delegate (bool status, NSError error) {
                        if (status)
                        {
                            Console.Write("Video added");
                            tcs.SetResult("success");
                        }
                    });

                }
try
            {
                webClient.DownloadFileAsync(new Uri(fileUri), videoFileName);
            }
            catch (Exception e)
            {
                tcs.SetException(e);
            }
            return await tcs.Task;
}

任何帮助将不胜感激。感谢。

1 个答案:

答案 0 :(得分:0)

  

(可可错误-1 /)

  • 您确定从下载中确实拥有有效的数据/ mp4吗?

  • 您使用的是SSL(https),否则您是否在info.plist中申请了ATS例外?

    • 检查手机/模拟器控制台输出是否有关于ATS的错误

注意:我通常直接使用NSUrlSession来避免HttpClient包装...

使用NSUrlSession任务的示例:

var videoURL = NSUrl.FromString(urlString);
var videoPath = NSFileManager.DefaultManager.GetUrls(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomain.User)[0];
NSUrlSession.SharedSession.CreateDownloadTask(videoURL, (location, response, createTaskError) =>
{
    if (location != null && createTaskError == null)
    {
        var destinationURL = videoPath.Append(response?.SuggestedFilename ?? videoURL.LastPathComponent, false);
        // If file exists, it is already downloaded, but for debugging, delete it...
        if (NSFileManager.DefaultManager.FileExists(destinationURL.Path)) NSFileManager.DefaultManager.Remove(destinationURL, out var deleteError);
        NSFileManager.DefaultManager.Move(location, destinationURL, out var moveError);
        if (moveError == null)
        {
            PHPhotoLibrary.RequestAuthorization((status) =>
            {
                if (status.HasFlag(PHAuthorizationStatus.Authorized))
                {
                    PHPhotoLibrary.SharedPhotoLibrary.PerformChanges(() =>
                    {
                        PHAssetChangeRequest.FromVideo(destinationURL);
                    }, (complete, requestError) =>
                     {
                        if (!complete && requestError != null)
                            Console.WriteLine(requestError);
                     });
                }
            });
        }
        else
            Console.WriteLine(moveError);
    }
    else
        Console.WriteLine(createTaskError);
}).Resume();

注意:要确认您的代码,请尝试使用已知的有效安全网址来源:

https://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4通过“Video For Everybody” Test Page