Loading image with AFNetworking - Resizing
afnetworking, ios, objective-c, uiimageview
Solution
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
}];
Problem
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.