Download Image Using AFNetworking - Resize

First of all, I use this AFNetworking method:

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

1 - Is this method asynchronous? Will it cache the image on the iPhone?

2 - How can I crop / resize this image? I have an 800x600 image in the url but my UIImageView is 400x400, I want the url image to be cropped before showing so that it is in the same ratio as 600x600 (it shouldn't have been 400x400, same ratio). Like a facebook app.

+6
source share
4 answers

Resizing was given elsewhere, but to your first question:

Is this method asynchronous?

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

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

Then you ask:

Will it cache the image on the iPhone?

If you're just looking for a cache in memory for performance reasons, then the answer is definitely yes. It uses a NSCache (which will be emptied under memory pressure). Aside, it will cache the image as extracted, not reflecting the resizing you make after the fact.

If you are looking for a cache in persistent storage (i.e. a cache that will persist even if you terminate the application and restart it), this question is a little less clear. AFNetworking claims to support caching through the use of NSURLCache , but I had problems working with iOS. If you need persistent storage caching, I would suggest several other UIImageView categories such as SDWebImage .

In any case, for the official AFNetworking line for caching, I can refer to the discussion of Caching in the AFNetworking FAQ.


If you want to see an activity indicator, 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 }]; 
+17
source

To get a cropping image:

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

Use the following method returning UIImage (since you want the image size)

 - (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 the Croped Image returned as described above;

OR RESIZING

And also use the following method for height 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 returns a NewImage , with the specific size you want.

+2
source

From AFNetworking v.2.0 there AFCoreImageSerializer

A response serializer used to create an image view from server response and response data. By default, this is an instance of AFImageResponseSerializer

AFImageResponseSerializer subclasses can be used to perform post-processing, such as color grading, face recognition, or other effects.

You can use this to crop the image before setting it to UIImageView .

0
source

I have a sample code that needs to be added to a Hemang answer.

This is for AFImageResponseSerializer . Just zoom in on the image you are extracting.

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

0
source

Source: https://habr.com/ru/post/949559/


All Articles