本文介绍了PHAsset + AFNetworking。无法将文件上传到真实设备上的服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前我正在使用以下代码将文件上传到服务器

Currently I'm using the following code to upload files to the server

NSURLRequest *urlRequest =  [[AFHTTPRequestSerializer serializer] multipartFormRequestWithMethod:@"POST" URLString:[[entity uploadUrl]absoluteString] parameters:entity.params constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {

    // Get file url 
    [UploadModel getAassetUrl:entity.asset resultHandler:^(NSURL *fileUrl) {

    NSError *fileappenderror;
    // Append
                [formData appendPartWithFileURL:fileUrl name:@"data" error:&fileappenderror];

                if (fileappenderror) {
                  [Sys MyLog: [fileappenderror localizedDescription] ];
                }
            }];

        } error:&urlRequestError];

/ * getAassetUrl * /

/*getAassetUrl */

+(void)getAassetUrl: (PHAsset*)mPhasset resultHandler:(void(^)(NSURL *imageUrl))dataResponse{

    PHImageRequestOptions * requestOption = [[PHImageRequestOptions alloc] init];
    requestOption.synchronous = YES;
    requestOption.deliveryMode = PHImageRequestOptionsDeliveryModeFastFormat;

        [[PHImageManager defaultManager] requestImageDataForAsset:mPhasset options:requestOption resultHandler:^(NSData *imageData, NSString *dataUTI, UIImageOrientation orientation, NSDictionary *info) {

        dataResponse([info objectForKey:@"PHImageFileURLKey"]);

    }];
}

此方法适用于模拟器,但在真实设备上失败:空文件上传到服务器很可能是由于无法从本地存储中读取。
日志显示通知

This approach works on a simulator, but fails on a real device: empty files are uploaded to the server most likely due to failure to read from the local storage. Log shows the notice

我相信这个笔记意味着app无法访问该文件按指定路径。
此外,我尝试了一种替代方法,通过附加NSData来上传文件,NSData是从请求PHAsset数据返回的。但是这种方法在大型媒体文件的情况下是不可用的。因为整个文件都被加载到内存中。

I believe this note means that app can't access the file by specified path.Also I've tried an alternative approach uploading file by appending with NSData which is returned from request PHAsset data. but this approach is unusable in case of large media files. since the entire file is loaded into the memory.

有什么想法?

推荐答案

<$>返回的NSData对象c $ c> requestImageDataForAsset 是内存映射的 - 所以整个文件没有加载到内存中。所以这个方法对于图像没有任何问题。

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.

对于视频,你应该使用适当的方法 requestExportSessionForVideo requestAVAssetForVideo
如果你可以将你的部署目标限制在iOS 9,你还应该看看 PHAssetResourceManager

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

这篇关于PHAsset + AFNetworking。无法将文件上传到真实设备上的服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-14 20:54