我是iOS的新手,并且遇到有关在Web Service的``图像视图''中显示图像的问题。我正在使用类似这样的代码
- (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;
}
图像路径- http://qaec.teams.in.net/UploadedFiles/Tommy舍费尔,他在杀害作用,为他所服务的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
不需要使用任何其他库等。您的代码在此行上是错误的:
[strImgURLAsString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
看看这有什么用吗?没有什么。您应该将变量strImgURLAsString分配给它,然后它应该可以工作:
strImgURLAsString = [strImgURLAsString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句