imageview图像填充适合模式

时间:2013-03-12 10:36:32

标签: ios objective-c

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

[cell viewWithTag:12345]

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

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

3 个答案:

答案 0 :(得分: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 ,具有您想要的特定大小。

答案 1 :(得分:0)

首先,我会检查图像视图的contentMode或图像视图的大小是否存在问题。由于自动调整遮罩,您的imageview也可能会调整大小。如果使用

初始化图像视图
- (id)initWithImage:(UIImage *)image

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

答案 2 :(得分: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;
}

OUT put: enter image description here

和案例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;
}

输出: enter image description here

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

谢谢

相关问题