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