我正在使用来自 URL 的图像创建一个表视图,但图像不会调整到所有视图的大小,直到我将其按入行中。
知道为什么会发生这种情况吗?
这是一个自定义的表格视图
我的代码是:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *CellIdentifier = @"Celula_Noticias";
Celula_Noticias *cell = (Celula_Noticias*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
NSArray * top= [[NSBundle mainBundle] loadNibNamed:@"Celula_Noticias" owner:self options:nil];
for(id currentObject in top){
if ([currentObject isKindOfClass:[Celula_Noticias class]]) {
cell= (Celula_Noticias *) currentObject;
break;
}
}
}
cell.Image_Noticias_1.image=[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:[[noticias objectAtIndex:indexPath.row ] objectAtIndex:0]]]];
cell.Image_Noticias_2.image=[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:[[noticias objectAtIndex:indexPath.row ] objectAtIndex:2]]]];
cell.Titulo_Noticias_1.text=[[noticias objectAtIndex:indexPath.row ] objectAtIndex:1];
cell.Titulo_Noticias_2.text=[[noticias objectAtIndex:indexPath.row ] objectAtIndex:3];
return cell;
}
表格已正确填充,但图像的开头尺寸不正确。
我尝试过使用 CGRectMake 但它仍然不起作用。
Thanks.
创建一个函数来对下载的图像进行后处理:
- (UIImage*)postProcessImage:(UIImage*)image
{
CGSize itemSize = CGSizeMake(50, 50);
UIGraphicsBeginImageContext(itemSize);
CGRect imageRect = CGRectMake(0.0, 0.0, itemSize.width, itemSize.height);
[image drawInRect:imageRect];
UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}
上面的函数会给你返回一个 50*50 的漂亮图像。
然后你可以这样做:
UIImage* downloadedImage = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:@"...url goes here..."]]];
UIImage* newImage = [self posProcessImage:downloadedImage];
cell.imageView.image=newImage
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)