PHAsset + AFNetworking. Unable to upload files to the server on a real device

与世无争的帅哥 提交于 2019-11-30 10:51:43

The NSData object returned by requestImageDataForAsset is memory mapped - so the entire file is not loaded into memory. So this method will for without any issues for images.

For videos you should use the appropriate methods requestExportSessionForVideo or requestAVAssetForVideo If you can limit your deployment target to iOS 9, you should also take a look at the methods of PHAssetResourceManager

You shouldn't use requestImageDataForAsset(_:options:resultHandler:) for large files. Reason being you don't want to load the entire media file into memory, you will quickly run out of memory and the app will crash. This typically means you shouldn't use it for large images or pretty much any video.

In my experience, attempting to upload directly from a PHAsset resource url will fail. Apple doesn't seem to grant us the permissions required to upload direct from PHAsset source files. See forum post here. This is a pain because it forces us to use a ton of extra disk space if we want to upload a video.

In order to get a local file url for a video file that you intend to upload, you'll want to use either:

requestExportSessionForVideo(_:options:exportPreset:resultHandler:)

or

requestAVAssetForVideo(_:options:resultHandler:)

You will use these methods to export a copy of the video file to a location on disk that you control. And upload from that file. Bonus feature: both of these methods will download the file from iCloud if necessary.

Check out the VimeoUpload library for details on all things related to video upload. Disclaimer: I'm one of the authors of the library.

Even if you're not uploading to Vimeo servers, you can use the PHAssetExportSessionOperation and ExportOperation classes included in VimeoUpload to do exactly what you're looking to do. See the repo README for details on obtaining a file url for a PHAsset. It also includes tools for obtaining a file url for an ALAsset.

If you're not interested in using PHAssetExportSessionOperation or ExportOperation, check out their implementations for details on how to use the Apple classes under the hood.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!