I'm working with AFNetworking to load my images for cell placeholder. Here is my code example. (Token from here)
NSURL *url = [NSURL URLWithString:daysWeather.weatherIconURL];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
UIImage *placeholderImage = [UIImage imageNamed:#"placeholder"];
__weak UITableViewCell *weakCell = cell;
[cell.imageView setImageWithURLRequest:request
placeholderImage:placeholderImage
success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image) {
NSLog(#"Load placeholder img");
weakCell.imageView.image = image;
[weakCell setNeedsLayout];
} failure:nil];
return cell;
And now I have a question. Does this images load every time, when it becomes visible? As you can see I've added NSLog(#"Load placeholder img"); in success block. And I see, that this code calls every time, when cell becomes visible (after it was visible, disappeared and become visible again)
Yes success block called every time. But AFNetworking will download the image only once and saved it in cache. Once you make request again for same URL or image it will directly load image from cache if available.
Also, if you don't want to do any specific operation in block, then simply use setImageWithURL:placeholderImage.
Related
I am setting image in UIImageView using:
[self.imageView setImageWithURL:[NSURL URLWithString:urlString] placeholderImage:[UIImage imageNamed:#"random_image"]];
I am able to see the image on phone as expected.
However, if I use the method without placeholder argument
[self.imageView setImageWithURL:[NSURL URLWithString:urlString]];
OR pass the placeholderImage as nil
[self.imageView setImageWithURL:[NSURL URLWithString:urlString] placeholderImage:nil];
then the image is not set and my imageView is blank. I understand that placeholder image is for "representing the current state of the asset suitable for temporarily displaying in your extension’s UI.".
I don't understand how its absence should impact me the way it is impacting. Any pointers?
If you set default placeholder image then imageview gets width and night.Otherwise imageview frame is may be (0,0).
Or
The image should be downloaded asynchronously using NSURLConnection, and only once it's fully downloaded should it be converted into a UIImage and assigned to the image view
[NSURLConnection sendAsynchronousRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:imageUrl]] queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
ImageViewName.image = [UIImage imageWithData:data];
}];
I found the issue. I added an image view in UITableViewCell with the name imageView. Apparently, UITableViewCell already has a property with the same name. I changed the name of myImageView to cellImageView and now, I don't need the placeholder parameter anymore! Having said that, I still don't understand what happened behind the screens!
i hope these code will help u....
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul);
dispatch_async(queue, ^{
UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:
[NSURL URLWithString:#“image URL”]]];
dispatch_sync(dispatch_get_main_queue(), ^{
[[cell myimage] setImage:image];
}
});
});
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.)
I Have copied the contents of file from here and created a UIImageView+AFNetworking.h and imported in my implementation file
Now When I Write the following code i get this error but when i remove the block of code then everything works fine.
I want to display image in a custom table cell.The url of image i am grabbing Through JSON
NSURLRequest *imageRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:[dictArray objectForKey:#"image"]]];
[cell.thumbnailImageView setImageWithURLRequest:imageRequest placeholderImage:nil
success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image){
NSLog(#"success");
cell.thumbnailImageView.image = image;
cell.thumbnailImageView.contentMode = UIViewContentModeScaleAspectFit;
cell.thumbnailImageView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
[cell setNeedsLayout];// To update the cell {if not using this, your image is not showing over cell.}
}failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error){
NSLog(#"Failure");}];
Here is the screen shot of the error
It Crashes after loading
Instead of using afnetworking You can just use dispatch method
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul);
dispatch_async(queue, ^{
NSString *url = [indexDic objectForKey:#"image"];
NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:url]];
UIImage *image = [UIImage imageWithData:imageData];
dispatch_async(dispatch_get_main_queue(), ^{
cellImg.image = image;
});
});
return cell;
}
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
I am using table view to show a list of data. Each custom table cell has a back ground image coming from JSON. I use AFNetworking framework to download those image. But table view scrolling is not smooth. I create those cells using .xib file.
static NSString *CellIdentifire=#"CellId";
NSMutableDictionary *murDic=[matchListarry objectAtIndex:indexPath.row];
TTTCellFormatchList *matchListcell;
matchListcell=(TTTCellFormatchList *)[tableView dequeueReusableCellWithIdentifier:CellIdentifire];
if (matchListcell==nil)
{
NSArray *CellNib=[[NSBundle mainBundle] loadNibNamed:#"TTTCellFormatchListcontroller" owner:self options:nil];
matchListcell=(TTTCellFormatchList *)[CellNib objectAtIndex:0];
}
UIView *MainView=(UIView *)[matchListcell.contentView viewWithTag:101];
UIActivityIndicatorView *Spinner=(UIActivityIndicatorView *)[MainView viewWithTag:93];
//Clearing The back Ground
matchListcell.backgroundColor=[UIColor clearColor];
//Downlod back ground image in block and background thread
NSString *BackgroundImageStgring=[murDic valueForKey:#"MatchImage"];
UIImageView *BackgrounImage=(UIImageView *)[matchListcell.contentView viewWithTag:100];
NSURLRequest *request_img = [NSURLRequest requestWithURL:[NSURL URLWithString:BackgroundImageStgring]];
AFImageRequestOperation *operation = [AFImageRequestOperation imageRequestOperationWithRequest:request_img, imageProcessingBlock:nil, success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image)
{
if(image!=nil)
{
image=[image blurredImageWithRadius:2 iterations:1 tintColor:[UIColor darkGrayColor]];
[BackgrounImage setImage:image];
[Spinner stopAnimating];
[Spinner setHidden:YES];
}
}
failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error) {
[[UIImage imageNamed:#"picture.png"] applyLightEffect];
[BackgrounImage setImage:[UIImage imageNamed:#"picture.png"]];
NSLog(#"Error %#",error);
}];
[operation start];
// call background queue for dynamic loading of data
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{
// load your dynamic data here
// call main queue here
dispatch_async(dispatch_get_main_queue(), ^{
// after loading data in background. use your downloaded data here.
});
});
Custom UITableView with UITextview is not scrolling smoothly in ios device