Incorrect size of Images in TableView

眉间皱痕 提交于 2019-12-23 22:25:54

问题


I am creating a table view with images from an URL, but the images don´t re-size to all the view until I presset it in the row. any idea why that is happening? This is a custom tableview

My code is :

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

}

the table is correctly fill, but the image dont start with the correct size.

I have tried use the CGRectMake but it still don t work.

Thanks.


回答1:


Create a function to post process the downloaded image:

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

The function above will give you back a nice image of 50*50.

Then you could do:

UIImage* downloadedImage = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:@"...url goes here..."]]];
UIImage* newImage = [self posProcessImage:downloadedImage];
cell.imageView.image=newImage



回答2:


I think, the UIImageView you are using, you should change it's property to aspect fit(if it is no so)




回答3:


I have solve the problem... My view inside the tableviewcell in the Xib file was bigger than the tableviewcell, so it begins incorrectly (i don t now why), but now the table is correctly fill..

Thanks to all



来源:https://stackoverflow.com/questions/6178579/incorrect-size-of-images-in-tableview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!