ios - 使用 AFNetworking 上传视频会导致内存问题
<p><p>我知道以前有人问过这个问题,但我无法从这些帖子中找出正确的方法。所以这是我上传导致内存问题的视频文件的代码:</p>
<pre><code> AFHTTPSessionManager *operationManager = ;
operationManager.responseSerializer=;
operationManager.responseSerializer.acceptableContentTypes = ;
//;
;
if( && != nil && != nil){
}
;
[operationManager POST:url parameters:dict constructingBodyWithBlock:^(id<AFMultipartFormData>_Nonnull formData) {
SAAppDelegate *appDelegate = ;
if( && appDelegate.imageData !=nil){
// ;
;
}
if( && appDelegate.imageData !=nil){
;
}
if( && appDelegate.imageData !=nil){
;
}
} progress:^(NSProgress * _Nonnull uploadProgress) {
} success:^(NSURLSessionDataTask * _Nonnull task, id_Nullable responseObject) {
arrayParsedJson =(NSMutableArray * )responseObject;
;
//
//;
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
}];
</code></pre>
<p>谁能解释一下这里出了什么问题?</p></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>在我看来,你应该使用方法</p>
<pre><code>- (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;
</code></pre>
<p>输入是 URLString,而不是 NSData。 AFNetworking 可以为您处理数据问题。这是我在上传太大视频时应用程序崩溃后的经验。希望这会有所帮助。</p>
<p>我的项目代码供大家引用</p>
<pre><code>- (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 ([ isEqualToString:@"mp4"]) {
if (!) {
_videoWriteAPIURL = [.fileAPIURLDict objectForKey:@"swrite_addr"];
}
}
void (^blk)(void) = [^{
AFHTTPRequestOperation *operation =
[ isEqualToString:@"mp4"] ? _videoWriteAPIURL : fileAPIURL)
andSelectedLocalUserProfileId:nil] POST:@"file"
parameters:parameters
constructingBodyWithBlock:
^(id<AFMultipartFormData> formData) {
NSError *error;
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 = ;
fileURL = ;
}
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 (.fileAPIURLDict == nil) {
[self getFileAPIURLCompleted:^(NSDictionary *newFileAPIURLDict, NCBServiceError *error) {
if (!error) {
fileAPIURL = ;
_videoWriteAPIURL = ;
blk();
} else {
completed(nil, error);
}
}];
} else {
if (.fileAPIURLDict objectForKey:@"write_addr"]]) {
fileAPIURL = [.fileAPIURLDict objectForKey:@"write_addr"];
_videoWriteAPIURL = [.fileAPIURLDict objectForKey:@"swrite_addr"];
blk();
} else {
[self getFileAPIURLCompleted:^(NSDictionary *newFileAPIURLDict, NCBServiceError *error) {
if (!error) {
fileAPIURL = ;
_videoWriteAPIURL = ;
blk();
} else {
completed(nil, error);
}
}];
}
}
}
</code></pre></p>
<p style="font-size: 20px;">关于ios - 使用 AFNetworking 上传视频会导致内存问题,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/40084703/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/40084703/
</a>
</p>
页:
[1]