gpt4 book ai didi

objective-c - AF网络进度方法

转载 作者:行者123 更新时间:2023-12-03 17:38:00 24 4
gpt4 key购买 nike

我正在使用 AFNetworking 发出 HTTP 请求:

AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
[manager GET:url parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {
int t=0;
t++;
} success:^(NSURLSessionTask *task, id responseObject) {
NSMutableArray * items = [self parseSearchCategoryWithData:responseObject];
finishBlock(items);
} failure:^(NSURLSessionTask *operation, NSError *error) {
finishBlock(nil);
}];

我想获取 url GET 的进度,但未调用 Progress block 。知道可能是什么问题吗?

最佳答案

我建议使用以下模板来加载带有进度的图像:

AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
manager.responseSerializer = [AFImageResponseSerializer serializer];

manager.requestSerializer.cachePolicy = NSURLRequestReloadIgnoringLocalAndRemoteCacheData;

[manager GET:url.absoluteString parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {

float progress = downloadProgress.fractionCompleted;

} success:^(NSURLSessionTask *task, id responseObject) {
UIImage *responseImage = responseObject;

} failure:^(NSURLSessionTask *operation, NSError *error) {
NSLog(Failed with error: %@", error);
}];

以及用于下载带有进度的视频的模板:

AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
NSURLSessionDownloadTask *downloadTask = [manager downloadTaskWithRequest:request progress:^(NSProgress * _Nonnull downloadProgress) {
float progress = downloadProgress.fractionCompleted;

} destination:^NSURL * _Nonnull(NSURL * _Nonnull targetPath, NSURLResponse * _Nonnull response) {

NSURL *documentsDirectoryURL = [[NSFileManager defaultManager] URLForDirectory:NSDocumentDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:NO error:nil];
return [documentsDirectoryURL URLByAppendingPathComponent:[response suggestedFilename]];

} completionHandler:^(NSURLResponse * _Nonnull response, NSURL * _Nullable filePath, NSError * _Nullable error) {

if(error) {
NSLog(Error %@", error);
}
else {
NSString *path = filePath.relativeString;
}
}
}];

[downloadTask resume];

希望这会有所帮助!

关于objective-c - AF网络进度方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40045029/

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