我在我的应用程序中使用 AFNetwork 从我的网络服务中获取一些数据。在这些数据中,有一些随机大小的图像,我需要添加一些 UIImageView。
我的问题是在异步模式下以 UIImageView的正确比例调整图像。
知道怎么做吗?
编辑
我正在使用 Afnetoking 项目的常规方法:
[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;
基本上这会以适当的比例设置图像,并切割所有边缘..