OStack程序员社区-中国程序员成长平台

标题: ios - 使用 AFNetworking 上传视频会导致内存问题 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 22:32
标题: ios - 使用 AFNetworking 上传视频会导致内存问题

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

    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/






欢迎光临 OStack程序员社区-中国程序员成长平台 (https://ostack.cn/) Powered by Discuz! X3.4