UITableView with image in cell stucks - ios

I am developing an app which use UITableView,which contains cell having UIImageView of size 320 X 200.
This images comes from the web url and store to my app folder.Then i am showing this image to user.
For that i am using "UIImageView+AFNetworking.h" class's below method.
- (void)setImageWithURL:(NSString *)url
placeholderImageName:(NSString *)placeholderImage
saveFilePath:(NSString *) filePath
IndexPath:(NSIndexPath *)indexPath
success:(void (^)(NSIndexPath *indexPath, UIImage *image))success
failure:(void (^)(NSIndexPath *indexPath, NSError *error))failure {
NSLog(#"DOWNLOAD IMG URL :: %#", url);
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:[url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]];
[request setHTTPShouldHandleCookies:NO];
[request setHTTPShouldUsePipelining:YES];
[request addValue:#"image/*" forHTTPHeaderField:#"Accept"];
[self setImageWithURLRequest:request
placeholderImage:placeholderImage == nil? nil : [UIImage imageNamed:placeholderImage]
success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image) {
[Storage SaveImage:image WithFileName:filePath];
if (success)
success(indexPath, image);
}
failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error) {
if (failure)
failure(indexPath, error);
}
];
}
when my all images downloaded to my local folder after that my tableview stucks while showing images.

It might be possible that the downloading happens in sync with the main thread and is blocking it until the download has finished.
I use SDWebImage in my projects for this. It work's really really well.
EDIT:
OK, AFNetworking is doing all the work asynchronously and also caches all the images. See the documentation.
If you don't need your success and failure blocks, you could go away with this.
- (void)setImageWithURL:(NSString *)url
placeholderImageName:(NSString *)placeholderImage {
NSURL *url = [NSURL URLWithString:[url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
UIImage *pImage = placeholderImage == nil? nil : [UIImage imageNamed:placeholderImage];
[self setImageWithURL:url placeholderImage:pImage];
}

Or maybe the image is extremely large (that's independent of the UIImageView size).
Can we see your code?
Do you load them manualy from the file system? Probably is that, because you do that in the main thread.
Try this in the code that loads the image:
NSLog(#"Is this the main thread: %d", [NSThread isMainThread]);
If you see 1, that's the problem. You can solve it using GCD (remember that once the image is loaded, move it to the main thread)

Use NSOperationQueue for downloading image. While adding new item in NSOperationQueue, first check if it already added or not.
After NSOperation complete, that image will save into local.
Always load image from locally. If image not present in local then display some place holder.
Hope it helps.
Regards,
Punita

Related

AFNetworking setImageWithURLRequest not working

I am unable to download the images, The setImageWithURLRequest method is not going inside the success/failure block. Please someone advise me how solve this task. This is my code.
__weak UIImageView *images ;
NSString *url =[NSString stringWithFormat:#"%#%#",imageBaseUrl,eachImage];
NSLog(#"image download url %#",url);
[images setImageWithURLRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:url]]
placeholderImage:nil
success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image) {
if(response){
[arr addObject:image];
NSLog(#"Success fetching image");
}else{
NSLog(#"The image data is not there");
}
} failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error) {
NSLog(#"Request failed with error: %#", error);
}];
I have checked above code and it is worked for other image URL. While i am used your image URL it is failed to load image.
if you add manually http:// to URL. Then image is load properly.
Your URL should be like http://3.0.191.16/uploads/patient/2133/38049.jpg
Your logic should be like below.
if url doesn't contain http then append http:// manually otherwise go with url from server.
Or you can ask API developer to send proper image URL.
Please mark as accept if it will work for you.

Set a downloaded image to Imageview from url - IoS

I have an url, clicking which it downloads the image. I want to set the image to the imageview. Currently have tried this but the image does not load. Can anyone suggest where i am going wrong.
Thanks in advance!
NSURL *url1 = [NSURL URLWithString:#"http://farm4.staticflickr.com/3695/9258420948_566a38bfdb_q_d.jpg"];
pic1.image = [UIImage imageWithCIImage:[CIImage imageWithContentsOfURL:url1]];
You want to use AFNetworking's category on UIImageView:
[myImageView setImageWithURLRequest:urlRequest placeholderImage:[UIImage imageNamed:#"placeholder.png"] success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image) {
myImageView.image = image;
} failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error) {
NSLog(#"Request failed with error: %#", error);
}];
It is worth working with AFNetworking just for this call. (AFNetworking also gives you a placeholder image, and it also caches network images so subsequent calls use the cache giving you a quick response.)

Retain cycle with AFNetworking

I'm writing a gallery with images which can be loaded by url with AFNetworking.
In Init method of the ImageView object I call a function that send a request. Here:
- (void) loadWithUrl:(NSURL *)url
{
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url
cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:TimeOut];
[request setHTTPShouldHandleCookies:NO];
[request setHTTPShouldUsePipelining:YES];
__weak AOWImageView *safeSelf = self;
m_operation = [AFImageRequestOperation imageRequestOperationWithRequest:request
imageProcessingBlock:nil
success:^
(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image)
{
[safeSelf setImage:image];
}
failure:^
(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error)
{
[safeSelf setNoImageLabelOpaque];
}];
[m_operation start];
}
If the ImageView is outside the visible part of the screen - (void) dealloc is called. I cancel operation loading image in this method so: [m_operation cancel];. I guess that the operations are not canceled because the memory is increasing and isn't released.
I think that there is retain cycle. I want to understand how to write it right. Thanks.
I don't see a retain cycle in the code snippet you posted, so if you are leaking memory, it may be due to another part of your app's code...
Regarding "best practices" for AFNetworking and loading images-
AFNetworking has a built in category on UIImageView that has convenience methods for setting an an image view's image via a URL.
See UIImageView+AFNetworking, specifically setImageWithURL: method and related. This also has the advantage of keeping a cache (so you don't have to fetch images again if requested multiple times), which AFAIK, doesn't appear to done by AFImageRequestOperation.

Image view not updating after fetching image from URL

I'm using AFNetworking category UIImageView+AFNetworking.h in order to download an image from S3. I get a pre-signed URLRequest and proceed to get the image. This is in a controller solely meant to display a photo:
- (void)viewWillAppear:(BOOL)animated
{
NSURLRequest *request = [BFAWSUploader downloadPhotoWithFileName:[NSString stringWithFormat:#"%#.jpg", _topicMessage.attachment_s3_name]];
MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
[_imageView setImageWithURLRequest:request placeholderImage:[UIImage imageNamed:#"Default.png"] success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image) {
NSLog(#"Pulled down image successfully");
[hud hide:YES];
} failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error) {
NSLog(#"Failed to pull down image");
}];
}
I see the log entry "Pulled down image successfully", but I'm not seeing the image view update after that. Instead, I have to leave the view controller and then go back in, and I'll see the cached entry.
From UIImageView+AFNetworking.h:
"If a success block is specified, it is the responsibility of the block to set the image of the image view before returning. If no success block is specified, the default behavior of setting the image with self.image = image is executed."
So, you need _imageView.image = image inside your success block.

AFNetworking+UIImageView placeholder image shows up, but not URL image

I'm trying to use the AFNetworking UIImageView call to load images from a URL as shown below:
[self.image setImageWithURL:[NSURL URLWithString:feed.imageURL] placeholderImage: [UIImage imageNamed:#"logo"]];
The placeholder image always shows up, but the actual image from "feed.imageURL" never does. I've verified that the URL is actually correct. I even hardcoded it to make sure, and still nothing.
My basic app setup is a tab controller...and in viewDidLoad, I call a method "fetchFeed" which performs the HTTP request to gather my JSON data.
My request block looks like:
AFJSONRequestOperation *operation = [AFJSONRequestOperation
JSONRequestOperationWithRequest:request
success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
[self parseDictionary:JSON];
isLoading = NO;
[self.tableView reloadData];
} failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) {
NSLog(#"Error: %#", error);
[self showNetworkError];
isLoading = NO;
[self.tableView reloadData];
}];
operation.acceptableContentTypes = [NSSet setWithObjects:#"application/json", #"text/json", #"text/javascript", #"text/html", nil];
[queue addOperation:operation];
Turns out the server I was requesting the image from was sending content-type "image/jpg" and by default AFNetworking does not support this file type.
I changed the class method in AFImageRequestOperation to look like:
+ (NSSet *)defaultAcceptableContentTypes {
return [NSSet setWithObjects:#"image/tiff", #"image/jpeg", #"image/gif", #"image/png", #"image/ico", #"image/x-icon" #"image/bmp", #"image/x-bmp", #"image/x-xbitmap", #"image/x-win-bitmap", #"image/jpg", nil];
}
and it fixed my problem.
You can manage to accept what content-type you want with this library simply changing the request like this:
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:yourURL];
[request addValue:#"image/*" forHTTPHeaderField:#"Accept"];
And call the AFNetworking method:
AFJSONRequestOperation *operation = [AFJSONRequestOperation
JSONRequestOperationWithRequest:request
success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
} failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) {
}];
This way you will be able to override the content-type without changing the library.
AFNetworking doesn't support image/jpg MIME TYPE by default.
You can support it without modifying the AFNetworking Library
[AFImageRequestOperation addAcceptableContentType:#"image/jpg"];
All operations that manipulate the UI must be performed on the main thread. So you may need to use 'performSelectorOnMainThread:' when reloading your tableview data in the completion block.
[self.tableView performSelectorOnMainThread:#selector(reloadData) withObject:nil waitUntilDone:NO]
I had a similar problem but it turned out that I was passing a URL which contained spaces in it. When I properly encoded the URL using stringByAddingPercentEscapesUsingEncoding: the images now load.

Resources