Reputation: 979
I am new in iOS and I am facing problem regarding to show image in the Image View from Web Service.I am using code like this
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *STI=@"STI";
NewsTableViewCell *cell = (NewsTableViewCell *)[tableView dequeueReusableHeaderFooterViewWithIdentifier:STI];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"NewsTableViewCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
cell.accessoryType=UITableViewCellAccessoryNone;
}
NSString *strImgURLAsString = [NewsImageArray objectAtIndex:indexPath.row];
[strImgURLAsString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *imgURL = [NSURL URLWithString:strImgURLAsString];
[NSURLConnection sendAsynchronousRequest:[NSURLRequest requestWithURL:imgURL] queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
if (!connectionError) {
img = [[UIImage alloc] initWithData:data];
if (img==nil) {
// img=[UIImage imageNamed:@"userimage.png"];
}
cell.newsimage.image=img;
// pass the img to your imageview
}else{
NSLog(@"%@",connectionError);
}
}];
cell.Headlbl.text=[NSString stringWithFormat:@"%@",[headarray objectAtIndex:indexPath.row]];
NSString *aux = [shortnamearray objectAtIndex:indexPath.row];
NSString * htmlString = @"<html><body>";
NSString *htmlString2=@"</body></html>";
NSString * NewString=[NSString stringWithFormat:@"%@%@%@",htmlString,aux,htmlString2];
NSAttributedString * attrStr = [[NSAttributedString alloc] initWithData:[NewString dataUsingEncoding:NSUnicodeStringEncoding] options:@{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType } documentAttributes:nil error:nil];
cell.bodylbl.attributedText=attrStr;
cell.bodylbl.textAlignment=NSTextAlignmentCenter;
cell.bodylbl.textColor=[UIColor whiteColor];
[cell.bodylbl setFont:[UIFont fontWithName:@"Arial" size:16]];
cell.backgroundColor = cell.contentView.backgroundColor;
// cell.bodylbl.text=[NSString stringWithFormat:@"%@",[shortnamearray objectAtIndex:indexPath.row]];
// cell.randrid.text=[NSString stringWithFormat:@"%@",[idarray objectAtIndex:indexPath.row]];
return cell;
}
Image Path - http://qaec.teams.in.net/UploadedFiles/Tommy Schaefer, of his role in the slaying, for which he is serving 18 year.png
http://qaec.teams.in.net/UploadedFiles/Tommy%20Schaefer,%20of%20his%20role%20in%20the%20slaying,%20for%20which%20he%20is%20serving%2018%20year.png
When Image Does not contain any gap it shows image But when the Image contain any gap it not show image.How to solve this problem.Thanks in Advance!
Upvotes: 1
Views: 207
Reputation: 31
You can use following method in UITableviewCell class. In your case add code in NewsTableViewCell.m When cell is created that time drowRect method is called. So in this method you need to set setClipsToBounds property of image view for which you setting the image to YES. This will solve your issue.
-(void)drawRect:(CGRect)rect
{
[super drawRect:rect];
self.profilePicImgView.layer.cornerRadius=self.profilePicImgView.frame.size.width/2;
[self.profilePicImgView setClipsToBounds:YES];
[self.profilePicImgView layoutIfNeeded];
[self.profilePicImgView setNeedsDisplay];
}
Upvotes: 3
Reputation: 7588
There is no need to use any other library etc. Your code is mistake on this line:
[strImgURLAsString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
Have a look at this what does it do? Nothing. You should reassign the variable strImgURLAsString to this then it should work:
strImgURLAsString = [strImgURLAsString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
Upvotes: 2
Reputation: 712
For better performance, you can try this library called SDWebImage.
[imageView sd_setImageWithURL:[NSURL URLWithString:@"http://qaec.teams.in.net/UploadedFiles/Tommy%20Schaefer,%20of%20his%20role%20in%20the%20slaying,%20for%20which%20he%20is%20serving%2018%20year.png"] placeholderImage:[UIImage imageNamed:@"placeholder.png"]];
Also You need to make sure that you have written this in your plist file
Upvotes: 1
Reputation: 4174
I know it's good to idea to load image url in Imageview like the way you have written for better understanding of process. But maintaining cache is also important thing to consider. For better performance, I recommend you to use a very good library called SDWebImage. It helps you out in loading images faster with it's cache mechanism. Try this out. With this library it's this much simple.
[imageView sd_setImageWithURL:[NSURL URLWithString:@"http://www.domain.com/path/to/image.jpg"]
placeholderImage:[UIImage imageNamed:@"placeholder.png"]];
Here is another one. If you are using AFNetworking in your app, use below code to do that.
AFHTTPRequestOperation *requestOperation = [[AFHTTPRequestOperation alloc] initWithRequest:urlRequest];
requestOperation.responseSerializer = [AFImageResponseSerializer serializer];
[requestOperation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"Response: %@", responseObject);
_imageView.image = responseObject;
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Image error: %@", error);
}];
[requestOperation start];
Upvotes: 0
Reputation: 3682
Use like this:
[NSURLConnection sendAsynchronousRequest:[NSURLRequest requestWithURL:imgURL] queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
if (!connectionError) {
dispatch_async(dispatch_get_main_queue(), ^{
img = [[UIImage alloc] initWithData:data];
if (img==nil) {
// img=[UIImage imageNamed:@"userimage.png"];
}
cell.newsimage.image=img;
// pass the img to your imageview
});
}else{
NSLog(@"%@",connectionError);
}
}];
Upvotes: 0