【问题标题】:Incorrect size of Images in TableViewTableView 中图像的大小不正确
【发布时间】:2011-09-04 22:26:01
【问题描述】:

我正在使用来自 URL 的图像创建一个表格视图,但是在我将其按在行中之前,图像不会重新调整到所有视图的大小。 知道为什么会这样吗? 这是一个自定义的 tableview

我的代码是:

- (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,但还是不行。

谢谢。

【问题讨论】:

  • 显示您从 url 检索图像的代码,并将其设置在图像视图上。

标签: iphone ios uitableview uiimageview uiimage


【解决方案1】:

创建一个函数对下载的图像进行后期处理:

- (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

【讨论】:

    【解决方案2】:

    我认为,您正在使用的UIImageView,您应该将它的属性更改为适合方面(如果不是这样)

    【讨论】:

      【解决方案3】:

      我已经解决了问题... 我在 Xib 文件中的 tableviewcell 内的视图比 tableviewcell 大,所以它开始不正确(我现在不知道为什么),但现在表格已正确填充..

      谢谢大家

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多