Loading image with AFNetworking - Resizing

11,762

Solution 1

The resizing has been answered elsewhere, but to your first question:

This method is asynchronous?

Yes, it is asynchronous. You can use the callback blocks if you want to process the image, e.g.:

[imageView setImageWithURLRequest:request
                 placeholderImage:nil
                          success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image) {

                              // do image resize here

                              // then set image view

                              imageView.image = image;
                          }
                         failure:nil];

You then ask:

It will cache the image in iPhone?

If you're just looking to cache in memory for performance reasons, then the answer is an unequivocal yes. It employs a NSCache (which will be emptied upon memory pressure). As an aside, it will cache the image as retrieved, not reflecting any resizing you do after the fact.

If you're looking to cache in persistent storage (i.e. a cache that will persist even if you terminate app and restart it), that question is a little less clear. AFNetworking claims to support disk caching through its use of NSURLCache, but I have had problems getting that to work on iOS. If you need persistent storage caching, I might suggest a variety of other UIImageView categories, such as SDWebImage.

Anyway, for the AFNetworking official line on caching, I might refer you to the Caching discussion in the AFNetworking FAQ.


If you want an activity indicator view, you can:

UIActivityIndicatorView *activityIndicatorView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
activityIndicatorView.center = self.imageView.center;
[self.view addSubview:activityIndicatorView];
[activityIndicatorView startAnimating];

[imageView setImageWithURLRequest:request
                 placeholderImage:nil
                          success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image) {

                              [activityIndicatorView removeFromSuperview];

                              // do image resize here

                              // then set image view

                              imageView.image = image;
                          }
                          failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error) {
                              [activityIndicatorView removeFromSuperview];

                              // do any other error handling you want here
                          }];

Solution 2

For Get Crop Image:

UIImage *croppedImg = nil;
CGRect cropRect = CGRectMake(AS YOu Need);
croppedImg = [self croppIngimageByImageName:self.imageView.image toRect:cropRect];

Use following method that return UIImage (as You want size of image)

- (UIImage *)croppIngimageByImageName:(UIImage *)imageToCrop toRect:(CGRect)rect
    {
        //CGRect CropRect = CGRectMake(rect.origin.x, rect.origin.y, rect.size.width, rect.size.height+15);

        CGImageRef imageRef = CGImageCreateWithImageInRect([imageToCrop CGImage], rect);
        UIImage *cropped = [UIImage imageWithCGImage:imageRef];
        CGImageRelease(imageRef);

        return cropped;
    }

Here you get Croped Image that return by above method;

OR RESIZING

And also Use following method for specific hight and width with image for Resizing UIImage:

+ (UIImage*)resizeImage:(UIImage*)image withWidth:(int)width withHeight:(int)height
{
    CGSize newSize = CGSizeMake(width, height);
    float widthRatio = newSize.width/image.size.width;
    float heightRatio = newSize.height/image.size.height;

    if(widthRatio > heightRatio)
    {
        newSize=CGSizeMake(image.size.width*heightRatio,image.size.height*heightRatio);
    }
    else
    {
        newSize=CGSizeMake(image.size.width*widthRatio,image.size.height*widthRatio);
    }


    UIGraphicsBeginImageContextWithOptions(newSize, NO, 0.0);
    [image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
    UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    return newImage;
}

This method return NewImage, with specific size that you want.

Share:
11,762
Daniel Campos
Author by

Daniel Campos

Updated on June 18, 2022

Comments

  • Daniel Campos
    Daniel Campos almost 2 years

    First of all, I'm using this AFNetworking method:

    [imageView setImageWithURL:[NSURL URLWithString:@"http://site.com/img.png"]];
    

    1 - This method is asynchronous? It will cache the image in iPhone?

    2 - How can I crop/resize this image? I have a 800x600 image in the URL, but my UIImageView is 400x400, I only want to have the url image cropped before being shown, to be the same ratio, like 600x600 (It's not needed to be 400x400, just same ratio). Like facebook app.