OGeek|极客世界-中国程序员成长平台

标题: iphone - TableView 中图像的大小不正确 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 16:25
标题: iphone - TableView 中图像的大小不正确

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

我的代码是:

- (UITableViewCell *)tableViewUITableView *)tableView    cellForRowAtIndexPathNSIndexPath *)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,但还是不行。

谢谢。



Best Answer-推荐答案


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

- (UIImage*)postProcessImageUIImage*)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

关于iphone - TableView 中图像的大小不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6178579/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://sqlite.in/) Powered by Discuz! X3.4