使用AFNetworking上传视频会导致内存问题 [英] Uploading video using AFNetworking causes memory issue

查看:158
本文介绍了使用AFNetworking上传视频会导致内存问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道之前曾有人问过这个问题,但我无法从这些帖子中找出正确的方法.因此,这是我的代码,用于上传会导致内存问题的视频文件:

I know this question has been asked before but I can't figure out the correct way from those posts. So here is my code to upload a video file that causes memory issues:

    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) {

    }];

任何人都可以向我解释这是怎么回事吗?

Can anyone explain me what's wrong here?

推荐答案

我认为您应该使用方法

- (AFHTTPRequestOperation *)POST:(NSString *)URLString
                      parameters:(id)parameters
       constructingBodyWithBlock:(void (^)(id <AFMultipartFormData> formData))block
                         success:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success
                         failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure;

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

Input is URLString, not NSData. AFNetworking can handle data issue for you. This is my experience after my application crashed when upload too large video. Hope this helpful.

我的项目代码供您参考

- (void)uploadFileWithPath:(NSString *)filePath fileName:(NSString*)fileName mimeType:(NSString*)mimeType parameters:(NSDictionary *)parameters progressBlock:(void (^)(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 makeServiceErrorIfNeededWithOperation:operation
                                                                                     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 makeServiceErrorIfNeededWithOperation:operation
                                                                                        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);
                }
            }];
        }
    }
}

这篇关于使用AFNetworking上传视频会导致内存问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆