setImageWithURLRequest:
在我的UIImage
对象上调用成功或失败处理程序时似乎都没有调用。SetImageWithURL
也没有设置我的形象。如果我使用setImageWithURL:
placeholder:
占位符已设置但来自 url 的图像永远不会被设置。
请原谅我没有尽快发布代码。我尝试使用两种方法(with AFNetworking
)来异步设置我的图像视图:
[_imageView setImageWithURL:[NSURL URLWithString:@"path/to/file"]];
[_imageView setImageWithURLRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.example.com/path/to/image.jpg"]] placeholderImage:[UIImage imageNamed:@"your_image_here.jpg"] success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image) {
NSLog(@"Your image request succeeded!");
} failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error) {
NSLog(@"Your image request failed...");
}];
我尝试过触发这些,viewDidLoad
但图像视图永远不会加载。
在查看了实际的函数声明后setImageWithURLRequest:
,我调用它时似乎没有执行成功或失败块。我已经NSLogs
在代码中放置了让你们了解发生了什么的想法:
- (void)setImageWithURLRequest:(NSURLRequest *)urlRequest
placeholderImage:(UIImage *)placeholderImage
success:(void (^)(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image))success
failure:(void (^)(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error))failure
{
NSLog(@"Log shows");
[self cancelImageRequestOperation];
NSLog(@"Log shows");
UIImage *cachedImage = [[[self class] af_sharedImageCache] cachedImageForRequest:urlRequest];
NSLog(@"Log shows");
if (cachedImage) {
NSLog(@"Log does not show");
self.image = cachedImage;
self.af_imageRequestOperation = nil;
if (success) {
success(nil, nil, cachedImage);
NSLog(@"Log does not show");
}
} else {
NSLog(@"Log shows");
self.image = placeholderImage;
AFImageRequestOperation *requestOperation = [[AFImageRequestOperation alloc] initWithRequest:urlRequest];
[requestOperation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
if ([[urlRequest URL] isEqual:[[self.af_imageRequestOperation request] URL]]) {
self.image = responseObject;
self.af_imageRequestOperation = nil;
NSLog(@"Log does not show");
}
if (success) {
success(operation.request, operation.response, responseObject);
NSLog(@"Log does not show");
}
[[[self class] af_sharedImageCache] cacheImage:responseObject forRequest:urlRequest];
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
if ([[urlRequest URL] isEqual:[[self.af_imageRequestOperation request] URL]]) {
self.af_imageRequestOperation = nil;
NSLog(@"Log does not show");
}
if (failure) {
failure(operation.request, operation.response, error);
NSLog(@"Log does not show");
}
}];
self.af_imageRequestOperation = requestOperation;
[[[self class] af_sharedImageRequestOperationQueue] addOperation:self.af_imageRequestOperation];