2

我在我的应用程序中使用 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;

基本上这会以适当的比例设置图像,并切割所有边缘..

4

3 回答 3

6

AFUIImageView类别的一个变体在异步图像请求完成时调用一个块。

您可以在成功块中调整返回图像的大小并将其分配给图像视图。看

- (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

调整图像大小后,请确保UIImageView使用 GCD 将图像分配到主队列上,因为该块将在后台线程中调用。

这是一个使用 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
}];
于 2012-11-13T13:10:12.687 回答
2

编辑:

如果我正确理解您的问题,您只需imageView.contentMode = UIViewContentModeScaleAspectFit在从 Afnetworking FW 获得的 ImageView 上进行设置。

或者您可以检查图像的实际大小:imageView.image.size并相应地设置 imageView 的框架。

于 2012-10-30T10:34:04.823 回答
0

这是AFImageResponseSerializer为了什么。只需更改图像比例以匹配您正在检索的图像比例。

AFImageResponseSerializer *imageResponseSerializer = [self.avatarImageView imageResponseSerializer]; [imageResponseSerializer setImageScale:1.0];

于 2015-07-09T18:38:37.140 回答