【问题标题】:Resize UIImage in in asynchronous mode, using AFNetworking使用 AFNetworking 在异步模式下调整 UIImage 的大小
【发布时间】:2012-10-19 15:55:29
【问题描述】:

我在我的应用程序中使用 AFNetwork 从我的网络服务中获取一些数据。 在这些数据中,有一些随机大小的图片,我需要添加一些UIImageView。

我的问题是在异步模式下以 UIImageView 的正确比例调整图像。

知道怎么做吗?

编辑

我正在使用 Afnewoking 项目的常规方法:

 [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;

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

【问题讨论】:

    标签: ios ios5 uiimageview uiimage afnetworking


    【解决方案1】:

    编辑:

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

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

    【讨论】:

    • 我不想调整 UIImageView 的大小,我需要更新 UImage 以根据 UIImageView 纵横比正确缩放...所有这些都使用 Afnetworking 框架..
    • 哦,好吧,我不知道那个框架。你能发布你用来创建图像视图的代码吗?在不知道自己在做什么的情况下很难提供帮助......
    • 当然可以。我刚刚编辑了我的问题。由于我不知道服务器提供的 UIImage 的大小,因此我不知道如何在此类中以 assinc 方式对其进行缩放...
    • 如果我使用该选项,UIImage 会在 UIImageView 内自行缩放,并且可能会出现 UIImage 周围的空白区域...
    • 抱歉我的回复晚了:(
    【解决方案2】:

    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
    

    图像调整大小后,请确保使用 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
    }];
    

    【讨论】:

    • 你不应该在块中强烈捕获self
    • 更新答案以防止捕获自我。
    • 使用 __typeof(self) 更新了与类无关的代码
    【解决方案3】:

    这就是AFImageResponseSerializer 的用途。只需更改图像比例以匹配您正在检索的图像比例。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-08-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-08
      • 2011-09-16
      • 2013-07-14
      • 2019-01-20
      相关资源
      最近更新 更多