Search code examples
iosios5uiimageviewuiimageafnetworking

Resize UIImage in in asynchronous mode, using AFNetworking


I am using AFNetwork in my application to get some data from my web service. In these data, there are some images with random sizes, I need to add some UIImageView.

My problem is to adjust the image in the correct scale of UIImageView, in asynchronous mode.

Any idea how to do this?

EDIT

I am using the normal method of the Afnetwoking´s projects:

 [self setImageWithURL:[NSURL URLWithString:url] placeholderImage:[UIImage imageNamed:@"img_my_nophoto"]];

The logic inside this method is :

- (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];
    }
}

The full class code: https://github.com/AFNetworking/AFNetworking/blob/master/AFNetworking/UIImageView%2BAFNetworking.m

SOLUTION

I solve my problem setting the UIImageView with

self.contentMode = UIViewContentModeScaleAspectFill;

self.clipsToBounds=YES;

basically this set the image with the proper scale, and cut all the edges..


Solution

  • Edit:

    If I understand your problem correctly, you just need to set imageView.contentMode = UIViewContentModeScaleAspectFit on the ImageView you get from the Afnetworking FW.

    Or you could check the actual size of the image with: imageView.image.size and set the imageView's frame accordingly.