我正在尝试添加Success和Error块来检查图像何时加载,以便我可以使用NSCache动态调整图像大小。但是当我尝试
时[scribbleImage setImageWithURL: [NSURL URLWithString:scribble[@"scribble_image"]] placeholderImage:[UIImage imageNamed:@"Default.png"] success:^(UIImage *image) {
NSLog(@"done");
} failure:^(NSError *error) {
NSLog(@"error %@",error);
}];
Xcode给我一个错误,说No visible @interface for 'UIImageView' declares the selector 'setImageWithURL:placeholderImage:success:failure:'
我不确定为什么。
PS。我正在导入UIImageView + AFNetworking.h并且没有成功和失败阻止工作正常
答案 0 :(得分:2)
这是因为没有方法setImageWithURL:placeholderImage:success:failure:
只有setImageWithURLRequest:placeholderImage:success:failure:
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:scribble[@"scribble_image"]]];
[scribbleImage setImageWithURLRequest:request placeholderImage:[UIImage imageNamed:@"Default.png"] success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image) {
NSLog(@"Done");
} failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error) {
NSLog(@"Failed with error: %@", error);
}];
如果你看看setImageWithURL:placeholderImage:
做了什么:
- (void)setImageWithURL:(NSURL *)url
placeholderImage:(UIImage *)placeholderImage
{
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[request setHTTPShouldHandleCookies:NO];
[request addValue:@"image/*" forHTTPHeaderField:@"Accept"];
[self setImageWithURLRequest:request placeholderImage:placeholderImage success:nil failure:nil];
}
你看到这个方法也用于内部获取。