TableView中图像的大小不正确

时间:2011-05-30 16:19:56

标签: iphone ios uitableview uiimageview uiimage

我正在创建一个包含来自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,但它仍然无法正常工作。

感谢。

3 个答案:

答案 0 :(得分:4)

创建一个函数来处理下载的图像:

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

答案 1 :(得分:0)

我认为,你正在使用UIImageView,你应该将它的属性更改为宽高比(如果不是这样)

答案 2 :(得分:0)

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

感谢所有

相关问题