我在我的应用程序中使用AFNetwork从我的Web服务获取一些数据。 在这些数据中,有一些随机大小的图像,我需要添加一些UIImageView。
我的问题是在异步模式下以调整UIImageView的正确比例图像。
知道怎么做吗?
修改
我正在使用Afnetwoking项目的常规方法:
[self setImageWithURL:[NSURL URLWithString:url] placeholderImage:[UIImage imageNamed:@"img_my_nophoto"]];
此方法中的逻辑是:
- (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
{
[self cancelImageRequestOperation];
UIImage *cachedImage = [[[self class] af_sharedImageCache] cachedImageForRequest:urlRequest];
if (cachedImage) {
self.image = cachedImage;
self.af_imageRequestOperation = nil;
if (success) {
success(nil, nil, cachedImage);
}
} else {
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;
}
if (success) {
success(operation.request, operation.response, responseObject);
}
[[[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;
}
if (failure) {
failure(operation.request, operation.response, error);
}
}];
self.af_imageRequestOperation = requestOperation;
[[[self class] af_sharedImageRequestOperationQueue] addOperation:self.af_imageRequestOperation];
}
}
完整的班级代码: https://github.com/AFNetworking/AFNetworking/blob/master/AFNetworking/UIImageView%2BAFNetworking.m
解
我解决了使用
设置UIImageView的问题self.contentMode = UIViewContentModeScaleAspectFill;
self.clipsToBounds = YES;
基本上用适当的比例设置图像,并切割所有边缘。
答案 0 :(得分:6)
AF UIImageView
类别的变体在异步图像请求完成时调用块。
您可以在成功块中调整返回的图像的大小,并将其指定给图像视图。见
- (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
调整图像大小后,请确保使用GCD将图像分配给主队列上的UIImageView
,因为将在后台线程中调用块。
这是一个使用UIIImageView并将其内容模式设置为AspectFill的示例。使用Core Graphics调整大小通常比使用UIImageView重新调整图像质量更好。
__weak typeof(self) weakSelf = self;
CGSize targetSize = self.targetImageView.bounds.size;
[self.targetImageView setImageWithURL:photoURL success:^(UIImage *image) {
CGFloat imageHeight = image.size.height;
CGFloat imageWidth = image.size.width;
CGSize newSize = targetSize;
CGFloat scaleFactor = targetSize.width / imageWidth;
newSize.height = imageHeight * scaleFactor;
UIGraphicsBeginImageContextWithOptions(newSize, NO, 0.0);
[image drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];
UIImage *small = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
dispatch_async(dispatch_get_main_queue(),^ {
weakSelf.targetImageView.image = small;
});
} failure:^(NSError *error) {
// Handel the error here
}];
答案 1 :(得分:2)
编辑:
如果我正确理解您的问题,您只需要在Afnetworking FW上的ImageView上设置imageView.contentMode = UIViewContentModeScaleAspectFit
。
或者您可以使用imageView.image.size
检查图像的实际尺寸,并相应地设置imageView的框架。
答案 2 :(得分:0)
这是AFImageResponseSerializer
的用途。只需更改图像比例即可匹配您要检索的图像比例。
AFImageResponseSerializer *imageResponseSerializer = [self.avatarImageView imageResponseSerializer];
[imageResponseSerializer setImageScale:1.0];