#import "AFNetworking.h"
UIImageView *imgV = [[UIImageView alloc] initWithFrame:self.view.bounds];
imgV.contentMode = UIViewContentModeScaleAspectFit;
[self.view addSubview:imgV];
NSString *urlStr = @"https://cdn2.jianshu.io/assets/web/nav-logo-4c7bbafe27adc892f3046e6978459bac.png";
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
[manager GET:urlStr parameters:nil headers:@{} progress:^(NSProgress * _Nonnull downloadProgress) {
NSLog(@"进度-%@", downloadProgress);
} success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
NSLog(@"%@-%@", task, responseObject);
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
NSLog(@"error = %@", error);
}];
报错
error = Error Domain=com.alamofire.error.serialization.response Code=-1016 "Request failed: unacceptable content-type: image/png" UserInfo={NSLocalizedDescription=Request failed: unacceptable content-type: image/png, NSErrorFailingURLKey=https://cdn2.jianshu.io/assets/web/nav-logo-4c7bbafe27adc892f3046e6978459bac.png, com.alamofire.serialization.response.error.data={length = 1500, bytes = 0x89504e47 0d0a1a0a 0000000d 49484452 ... 49454e44 ae426082 }, com.alamofire.serialization.response.error.response= ......
修改
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
// 使用NSSet返回一个无序的序列化器
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"image/png", nil];
[manager GET:urlStr parameters:nil headers:@{} progress:^(NSProgress * _Nonnull downloadProgress) {
NSLog(@"进度-%@", downloadProgress);
} success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
NSLog(@"%@-%@", task, responseObject);
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
NSLog(@"error = %@", error);
}];
报错
error = Error Domain=NSCocoaErrorDomain Code=3840 "JSON text did not start with array or object and option to allow fragments not set." UserInfo={NSDebugDescription=JSON text did not start with array or object and option to allow fragments not set.}
修改
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
manager.responseSerializer = [AFHTTPResponseSerializer serializer];
// 使用NSSet返回一个无序的序列化器
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"image/png", nil];
[manager GET:urlStr parameters:nil headers:@{} progress:^(NSProgress * _Nonnull downloadProgress) {
NSLog(@"进度-%@", downloadProgress);
} success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
NSLog(@"%@-%@", task, responseObject);
// 把图片显示出来
imgV.image = [UIImage imageWithData:responseObject];
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
NSLog(@"error = %@", error);
}];
控制台打印:
LocalDataTask <FCDF08EF-7EBB-4A10-9DA5-5A153C0A2485>.<1>-{length = 1500, bytes = 0x89504e47 0d0a1a0a 0000000d 49484452 ... 49454e44 ae426082 }
查看当前线程
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
manager.responseSerializer = [AFHTTPResponseSerializer serializer];
// 使用NSSet返回一个无序的序列化器
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"image/png", nil];
[manager GET:urlStr parameters:nil headers:@{} progress:^(NSProgress * _Nonnull downloadProgress) {
NSLog(@"进度-%@", downloadProgress);
} success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
NSLog(@"%@-%@", task, responseObject);
NSLog(@"当前线程:%@", [NSThread currentThread]);
// 异步
imgV.image = [UIImage imageWithData:responseObject];
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
NSLog(@"error = %@", error);
}];
控制台
当前线程:<NSThread: 0x600001df9040>{number = 1, name = main}
AFN内部处理了,最后的block会在主线程回调
简化修改
// manager.responseSerializer = [AFHTTPResponseSerializer serializer];
// // 使用NSSet返回一个无序的序列化器
// manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"image/png", nil];
manager.responseSerializer = [AFImageResponseSerializer serializer];
报错
-[UIImage length]: unrecognized selector sent to instance 0x600000eacab0
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIImage length]: unrecognized selector sent to instance 0x600000eacab0'
修改
// imgV.image = [UIImage imageWithData:responseObject];
imgV.image = responseObject;
控制台
LocalDataTask <9CCD6132-221F-4D44-B45D-BE95AEC8109F>.<1>-<UIImage:0x600002ed46c0 anonymous ... ...
当前线程:<NSThread: 0x6000009d40c0>{number = 1, name = main}
修改
#import "UIKit+AFNetworking.h"
UIImageView *imgV = [[UIImageView alloc] initWithFrame:self.view.bounds];
imgV.contentMode = UIViewContentModeScaleAspectFit;
[self.view addSubview:imgV];
NSString *urlStr = @"https://cdn2.jianshu.io/assets/web/nav-logo-4c7bbafe27adc892f3046e6978459bac.png";
[imgV setImageWithURL:[NSURL URLWithString:urlStr] placeholderImage:nil];
框架的设计
#import "AFAutoPurgingImageCache.h"
@interface AFCachedImage : NSObject
@property (nonatomic, strong) UIImage *image;
@property (nonatomic, copy) NSString *identifier;// 方便找到
@property (nonatomic, assign) UInt64 totalBytes;//
@property (nonatomic, strong) NSDate *lastAccessDate;
@property (nonatomic, assign) UInt64 currentMemoryUsage;
@end