0

我有一个表格视图单元格,[原型],我在其中添加了一个图像视图。现在我在尺寸检查器中手动设置了这个图像视图的高度和宽度。但是我正在使用

[cell viewWithTag:12345]

在我的目标 c 代码中获取视图指针,并更新图像视图的图像。但是我发现我的手动约束被覆盖了,并且图像具有自己的大小。我在属性检查器中和以编程方式尝试了各种模式(Aspect fill、Aspect Fit 等)。

所以我的问题是如何设置约束?我哪里错了?为什么他们不工作?

4

3 回答 3

3

对图像的特定高度宽度使用以下方法

+ (UIImage*)resizeImage:(UIImage*)image withWidth:(int)width withHeight:(int)height
{
    CGSize newSize = CGSizeMake(width, height);
    float widthRatio = newSize.width/image.size.width;
    float heightRatio = newSize.height/image.size.height;

    if(widthRatio > heightRatio)
    {
        newSize=CGSizeMake(image.size.width*heightRatio,image.size.height*heightRatio);
    }
    else
    {
        newSize=CGSizeMake(image.size.width*widthRatio,image.size.height*widthRatio);
    }


    UIGraphicsBeginImageContextWithOptions(newSize, NO, 0.0);
    [image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
    UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    return newImage;
}

此方法返回NewImage,具有您想要的特定大小。

于 2013-03-12T10:41:13.137 回答
0

以下代码很有帮助

情况1:

   -(float)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 100.0;
}
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
        UIImageView *image = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
        image.tag = 101;
        [cell addSubview:image];
    }
    UIImageView *imageObj =(UIImageView *)[cell viewWithTag:101];
    [imageObj setImage:[UIImage imageNamed:@"url.jpeg"]];

    // Set up the cell...

    return cell;
}

输出: 在此处输入图像描述

案例2:

-(float)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 200.0;
}
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
        UIImageView *image = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 200, 200)];
        image.tag = 101;
        [cell addSubview:image];
    }
    UIImageView *imageObj =(UIImageView *)[cell viewWithTag:101];
    [imageObj setImage:[UIImage imageNamed:@"url.jpeg"]];

    // Set up the cell...


    return cell;
}

输出: 在此处输入图像描述

实际图像尺寸为 460 x 246 但您可以根据帧找到差异

谢谢

于 2013-03-12T10:54:22.320 回答
0

首先,我会检查问题是否出在图像视图的内容模式或图像视图的大小上。由于自动调整掩码,您的图像视图也可能被调整大小。如果你初始化你的图像视图

- (id)initWithImage:(UIImage *)image

方法您的 imageview 将自动调整为图像的大小。

于 2013-03-12T10:41:24.513 回答