gpt4 book ai didi

ios - 使用 AFNetworking 上传多个文件 - UIViewController 未释放

转载 作者:行者123 更新时间:2023-11-29 03:59:54 24 4
gpt4 key购买 nike

我必须上传多个文件,跟踪其进度并订阅完成和失败 block ,以便在操作结束时显示相关消息。

我编写了自己的 AFHTTPClient 包装器并创建了以下方法。

- (void) uploadFiles:(NSArray*)files
path:(NSString*)path
parameters:(NSDictionary*)parameters
progressBlock:(void (^)(NSUInteger bytesWritten, long long totalBytesWritten, long long totalBytesExpectedToWrite))block
success:(void (^)(AFHTTPRequestOperation *, id))success failure:(void (^)(AFHTTPRequestOperation *, NSError *))failure
{
NSMutableURLRequest *request =
[self multipartFormRequestWithMethod:@"POST"
path:path
parameters:parameters
constructingBodyWithBlock:
^(id <AFMultipartFormData>formData) {

for (CRLMultiPartFile *file in files) {
NSAssert(file.name, @"Name cannot be nil");
NSAssert(file.file, @"Nothing found to upload");
NSAssert(file.filename, @"FileName cannot be nil");
NSAssert(file.mimeType, @"Must set Mime-Type for %@", file.filename);
[formData appendPartWithFileData:file.file name:file.name fileName:file.filename mimeType:file.typeString];
}
}];

AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
[operation setUploadProgressBlock:block];
[operation setCompletionBlockWithSuccess:success failure:failure];
[self enqueueHTTPRequestOperation:operation];
}

调用此方法的 View Controller 不会被释放,因此包含的所有图像也保留在内存中,从而导致内存泄漏并最终导致内存警告。

进行分析后发现,在整个操作结束时, View Controller 的 refCount 为 1。

当我注释掉上传文件的调用时,一切正常。

这是 Controller 中的代码。它使用进度 block 来更新 UI 上的元素。

NSMutableDictionary *params = [[NSMutableDictionary alloc] init];
ContactModel *model = (ContactModel*)[self.contacts lastObject];
[params setObject:model.phone forKey:@"receiver"];

__block typeof(self) sSelf = self;

[[JMClient sharedClient] uploadFiles:files
path:@"picture_share/"
parameters:params
progressBlock:^(NSUInteger bytesWritten, long long totalBytesWritten, long long totalBytesExpectedToWrite) {

CGFloat progPercent = ceilf(((CGFloat)totalBytesWritten / (CGFloat)totalBytesExpectedToWrite) * 100);

CGFloat widthToCut = (progPercent * sSelf.progWidth) / 100;

CGRect frame = sSelf.progresViewBG.frame;
frame.size.width = (sSelf.progWidth - widthToCut);
frame.origin.x = (sSelf.progOrigin + widthToCut);
sSelf.progresViewBG.frame = frame;

sSelf.progLabel.text = [NSString stringWithFormat:@"%i%%", (int)progPercent];

frame = sSelf.progTipView.frame;
frame.origin.x = (sSelf.progresViewBG.frame.origin.x - frame.size.width/2);
sSelf.progTipView.frame = frame;

frame = sSelf.progLabel.frame;
frame.origin.x = (sSelf.progresViewBG.frame.origin.x - frame.size.width/2);
sSelf.progLabel.frame = frame;

} success:^(AFHTTPRequestOperation *success, id reponse) {

CGRect frame = sSelf.progresViewBG.frame;
frame.size.width = 0;
frame.origin.x = sSelf.progOrigin;
sSelf.progresViewBG.frame = frame;
[sSelf.cancelButton setImage:[UIImage imageNamed:@"trnsfr_prgss_complt.png"] forState:UIControlStateNormal];

[sSelf performSelector:@selector(hideAwayProgressBars) withObject:nil afterDelay:3];

} failure:^(AFHTTPRequestOperation *failure, NSError *error) {

[Mediator showMessage:TGLocalizedString(kMessageKeyForUploadingFailed)];
[sSelf performSelector:@selector(hideAwayProgressBars) withObject:nil afterDelay:3];

}];

self.operation = [[self.client.sharedClient.operationQueue operations] lastObject];

- (void) hideAwayProgressBars
{
[[NSNotificationCenter defaultCenter] postNotificationName:kNotifcationKeyForPhotoUploadComplete object:nil];
}

父 Controller 收到通知,从 super View 中删除该 Controller 的 View 并将其设置为 nil。

附言CRLMultiPartFile 是一个自定义类,用于保存要上传的文件的属性

最佳答案

如果您使用 ARC,则应使用 __weak 而不是 __block,这样就不会在 block 内捕获 self

关于ios - 使用 AFNetworking 上传多个文件 - UIViewController 未释放,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16008408/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com