简体   繁体   English

在表格视图单元格中调整图像大小

[英]resize image in table view cell

resize a image in table view cell... i am new programmer 在表视图单元格中调整图像大小...我是新程序员

by this code image comes in cell... but i wants resize image according to my need.... 通过此代码图像进入单元格...但我想根据我的需要调整图像大小....

if possible give me little code... thanks in advance... 如果可能的话,给我一点代码...预先感谢...

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:[NSString stringWithFormat:@"Cell %i",indexPath.section]];

    if (cell == nil) {
        //.......
    }
    search_items *pro = [result objectAtIndex:0];

    if(indexPath.row==0)
    {

    NSString *filepath=[[NSBundle mainBundle]pathForResource:pro.s_image ofType:@"png"];

    UIImage *image=[UIImage imageWithContentsOfFile:filepath];

    cell.imageView.contentMode = UIViewContentModeScaleAspectFit;

    //[cell.imageView setContentMode:UIViewContentModeScaleAspectFit];

    [cell.imageView setFrame:CGRectMake(50,50,100,100)];

    /*
    CGRect iframe = cell.imageView.frame;
    iframe.size.height = 100.0;
    iframe.size.width=300.0;

    cell.imageView.frame = iframe;

    */


    cell.imageView.image=image; 
    }
}

Rather than resizing the cell.imageView frame, try resizing the image before assigning it to cell.imageView.image: 而不是调整cell.imageView框架的大小,而是在将其分配给cell.imageView.image之前尝试调整其大小:

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString * cellIdentifier = @"cellIdentifier";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];

    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }

    cell.textLabel.text = @"My Cell's Title Text";
    cell.detailTextLabel.text = @"My Cell's Description Text";

    //### SETTING THE IMAGE HERE###
    //Let's say I want to resize my image entitled "myImage.png" to be 50x50 pixels in the table cell
    CGSize size = {50,50};
    cell.imageView.image = [self imageWithImage:[UIImage imageNamed:@"myImage.png" scaledToSize:size];

    return cell;
}

//Given a UIImage and a CGSize, this method will return a resized UIImage.
- (UIImage*)imageWithImage:(UIImage*)image
          scaledToSize:(CGSize)newSize;
{
    UIGraphicsBeginImageContext( newSize );
    [image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
    UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    return newImage;
}

Sources: 资料来源:

You can change the frame of the imageView like this: 您可以像这样更改imageView的框架:

CGRect frame = imageView.frame;
frame.size.height = 30.0;
/* other frame changes ... */
imageView.frame = frame;

You should also look at the UIImageView contentMode property (see this post ). 你也应该看看的UIImageView contentMode属性(见这个职位 )。 It gives you control over how the image is drawn, and is actually a UIView property (so UIImageView inherits it). 它使您可以控制图像的绘制方式,它实际上是一个UIView属性(因此UIImageView继承了它)。 It gives you the following options: 它为您提供以下选项:

Specifies how a view adjusts its content when its size changes. 指定视图大小改变时如何调整其内容。

typedef enum {
   UIViewContentModeScaleToFill,
   UIViewContentModeScaleAspectFit,
   UIViewContentModeScaleAspectFill,
   UIViewContentModeRedraw,
   UIViewContentModeCenter,
   UIViewContentModeTop,
   UIViewContentModeBottom,
   UIViewContentModeLeft,
   UIViewContentModeRight,
   UIViewContentModeTopLeft,
   UIViewContentModeTopRight,
   UIViewContentModeBottomLeft,
   UIViewContentModeBottomRight,
} UIViewContentMode;

You should use the backgroundView property of your cell and add a stretchable image : 您应该使用单元格的backgroundView属性并添加可拉伸图像:

UIImage* backgroundImage = [[UIImage imageNamed:@”stretchableImage.png”] stretchableImageWithLeftCapWidth:44 topCapHeight:45];

Hope this helps, Vincent 希望对您有帮助,文森特

1°) Well, by resizing, you mean which is scaled properly? 1°)那么,通过调整大小,您的意思是正确缩放? You should check the contentMode property of the UIImageView class [ cell.imageView setContentMode: ]; 您应该检查UIImageView类的contentMode属性[cell.imageView setContentMode:];

2°) If you want to resize the frame, then just do [ cell.imageView setFrame:CGRectMake() ]; 2°)如果要调整框架的大小,则只需执行[cell.imageView setFrame:CGRectMake()];

Depends what's really your problem ^^ 取决于您真正的问题是^^

Good Luck. 祝好运。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM