这是来自UIImageView+AFNetworking
库。
AFImageRequestOperation *requestOperation = [[AFImageRequestOperation alloc] initWithRequest:urlRequest];
[requestOperation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
if ([[urlRequest URL] isEqual:[[self.af_imageRequestOperation request] URL]]) {
if (success) {
success(operation.request, operation.response, responseObject);
} else {
self.image = responseObject;
[self setNeedsLayout];
[self setNeedsDisplay];
}
self.af_imageRequestOperation = nil;
}
我添加了setNeedsLayout
和setNeedsDisplay
。在这种情况下,imageview(UITableViewCell
的imageView仍然具有0,0,0,0
帧。单击单元格将刷新图像视图,否则图像不会显示。
如何强制UIImageView
调整大小?
我试过放入
self.image = responseObject;
self.frame = CGRectMake(self.frame.origin.x, self.frame.origin.y,
self.image.size.width, self.image.size.height);
[self setNeedsDisplay];
[self setNeedsLayout];
基于
How to make UIImageView automatically resize to the size of the image loaded
设置框架的解决方案。我可以在那里注销一个更大的框架,但它不会出现在屏幕上。
答案 0 :(得分:2)
在没有看到其余代码的情况下很难分辨,但我相信问题可能出在请求操作的完成处理程序中:
if (success) {
success(operation.request, operation.response, responseObject);
} else {
self.image = responseObject;
[self setNeedsLayout];
[self setNeedsDisplay];
}
您似乎只在请求失败时设置图像。将其移至成功块可能会解决问题。
if (success) {
success(operation.request, operation.response, responseObject);
self.image = responseObject;
[self setNeedsLayout];
[self setNeedsDisplay];
}
您是否有特殊原因没有使用AFNetworking / UIImageView + AFNetworking.h方法?似乎它们适合您的用例。
- (void)setImageWithURL:(NSURL *)url
placeholderImage:(nullable UIImage *)placeholderImage;
- (void)setImageWithURLRequest:(NSURLRequest *)urlRequest
placeholderImage:(nullable UIImage *)placeholderImage
success:(nullable void (^)(NSURLRequest *request, NSHTTPURLResponse * _Nullable response, UIImage *image))success
failure:(nullable void (^)(NSURLRequest *request, NSHTTPURLResponse * _Nullable response, NSError *error))failure;