• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

ios - 使用 AFNetworking 上传视频会导致内存问题

[复制链接]
菜鸟教程小白 发表于 2022-12-12 22:32:52 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

我知道以前有人问过这个问题,但我无法从这些帖子中找出正确的方法。所以这是我上传导致内存问题的视频文件的代码:

    AFHTTPSessionManager *operationManager = [AFHTTPSessionManager manager];
    operationManager.responseSerializer=[AFJSONResponseSerializer serializer];
    operationManager.responseSerializer.acceptableContentTypes = [NSSet setWithObject"text/html"];
    //[operationManager.requestSerializer setValue"application/json" forHTTPHeaderField"Content-Type"];
    [operationManager.operationQueue cancelAllOperations];
    if([requestName isEqualToString:addComment_Url] && [dict valueForKey"image_Data"] != nil && [dict valueForKey"mime_type"] != nil){
    }


    [operationManager.requestSerializer setValue:authValue forHTTPHeaderField"Authorization"];
    [operationManager POST:url parameters:dict constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData) {

        SAAppDelegate *appDelegate = [SAAppDelegate getDelegate];

        if([requestName isEqualToString:addComment_Url] && appDelegate.imageData !=nil){

           // [formData appendPartWithFormData:self.imageData name"myFile"];
            [formData appendPartWithFileData:appDelegate.imageData name"myFile" fileName:appDelegate.fileName mimeType:appDelegate.mime];
        }

        if([requestName isEqualToString:addNewPost_Url] && appDelegate.imageData !=nil){
            [formData appendPartWithFileData:appDelegate.imageData name"myFile" fileName:appDelegate.fileName mimeType:appDelegate.mime];
        }

        if([requestName isEqualToString:send_message_Url] && appDelegate.imageData !=nil){
            [formData appendPartWithFileData:appDelegate.imageData name"myFile" fileName:appDelegate.fileName mimeType:appDelegate.mime];
        }

    } progress:^(NSProgress * _Nonnull uploadProgress) {

    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        arrayParsedJson =  (NSMutableArray * )responseObject;

        [self.delegate dataReceivedFromService:arrayParsedJson withRequestName:requestName];
       //
        //[hud hideAnimated:YES];

    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {

    }];

谁能解释一下这里出了什么问题?



Best Answer-推荐答案


在我看来,你应该使用方法

- (AFHTTPRequestOperation *)POSTNSString *)URLString
                      parametersid)parameters
       constructingBodyWithBlockvoid (^)(id <AFMultipartFormData> formData))block
                         successvoid (^)(AFHTTPRequestOperation *operation, id responseObject))success
                         failurevoid (^)(AFHTTPRequestOperation *operation, NSError *error))failure;

输入是 URLString,而不是 NSData。 AFNetworking 可以为您处理数据问题。这是我在上传太大视频时应用程序崩溃后的经验。希望这会有所帮助。

我的项目代码供大家引用

- (void)uploadFileWithPathNSString *)filePath fileNameNSString*)fileName mimeTypeNSString*)mimeType parametersNSDictionary *)parameters progressBlockvoid (^)(CGFloat progress))progressBlock completed:(void (^)(NSString *fileURL, NCBServiceError *error))completed {
    if ([[parameters objectForKey:@"type"] isEqualToString:@"mp4"]) {
        if (![NCBUtil isGoodString:_videoWriteAPIURL]) {
            _videoWriteAPIURL = [[NCBSystemInfo sharedInstance].fileAPIURLDict objectForKey:@"swrite_addr"];
        }
    }
    void (^blk)(void) = [^{
        AFHTTPRequestOperation *operation =
        [[self fileClientWithURL:([[parameters objectForKey:@"type"] isEqualToString:@"mp4"] ? _videoWriteAPIURL : fileAPIURL)
   andSelectedLocalUserProfileId:nil] POST:@"file"
         parameters:parameters
         constructingBodyWithBlock:
         ^(id<AFMultipartFormData> formData) {
             NSError *error;
             [formData appendPartWithFileURL:[NSURL fileURLWithPath:filePath] name:@"UploadFile" fileName:fileName mimeType:mimeType error:&error];
         }
         success:
         ^(AFHTTPRequestOperation *operation, id responseObject) {
             NSString *fileURL = nil;

             NCBServiceError *serviceError = [NCBServiceError makeServiceErrorIfNeededWithOperationperation
                                                                                     responseObject:responseObject];

             if(!serviceError) {
                 NSDictionary *dict = [responseObject objectForKey:JsonResponseKeyResult];
                 fileURL = [dict objectForKey:@"file_url"];
             }
             if (completed) {
                 completed(fileURL, serviceError);
             }
         }
         failure:
         ^(AFHTTPRequestOperation *operation, NSError *error) {
             NCBServiceError *serviceError = [NCBServiceError makeServiceErrorIfNeededWithOperationperation
                                                                                        withNSError:error];
             if (completed) {
                 completed(nil, serviceError);
             }
         }];

        if(progressBlock) {
            [operation setUploadProgressBlock:
             ^(NSUInteger bytesWritten, long long totalBytesWritten, long long totalBytesExpectedToWrite) {
                 CGFloat progress = ((CGFloat)totalBytesWritten) / totalBytesExpectedToWrite;
                 progressBlock(progress);
             }];
        }
    } copy];

    if ([NCBSystemInfo sharedInstance].fileAPIURLDict == nil) {
        [self getFileAPIURLCompleted:^(NSDictionary *newFileAPIURLDict, NCBServiceError *error) {
            if (!error) {
                fileAPIURL = [newFileAPIURLDict objectForKey:@"write_addr"];
                _videoWriteAPIURL = [newFileAPIURLDict objectForKey:@"swrite_addr"];
                blk();
            } else {
                completed(nil, error);
            }
        }];
    } else {
        if ([NCBUtil isGoodString:[[NCBSystemInfo sharedInstance].fileAPIURLDict objectForKey:@"write_addr"]]) {
            fileAPIURL = [[NCBSystemInfo sharedInstance].fileAPIURLDict objectForKey:@"write_addr"];
            _videoWriteAPIURL = [[NCBSystemInfo sharedInstance].fileAPIURLDict objectForKey:@"swrite_addr"];
            blk();
        } else {
            [self getFileAPIURLCompleted:^(NSDictionary *newFileAPIURLDict, NCBServiceError *error) {
                if (!error) {
                    fileAPIURL = [newFileAPIURLDict objectForKey:@"write_addr"];
                    _videoWriteAPIURL = [newFileAPIURLDict objectForKey:@"swrite_addr"];
                    blk();
                } else {
                    completed(nil, error);
                }
            }];
        }
    }
}

关于ios - 使用 AFNetworking 上传视频会导致内存问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40084703/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap