iphone - 在表格 View 单元格中调整图像大小

标签 iphone ios

在表格 View 单元格中调整图像大小...我是新程序员

通过此代码图像进入单元格......但我想根据我的需要调整图像大小......

如果可能的话给我一些代码... 提前致谢...

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

最佳答案

与其调整 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;
}

来源:

关于iphone - 在表格 View 单元格中调整图像大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8026315/

相关文章:

ios - 如何调整 UIScrollView 的大小以适应子 UITextViews 的内容

iphone - didSelectRowAtIndexPath 返回错误的 IndexPath

iphone - 命令/usr/bin/codesign 失败,退出代码为 1

ios - 标签栏和导航 Controller 应用程序中的状态恢复

ios - 我们可以为 iPad 而不是 iPhone 显示 Settings.bundle 设置吗?

ios - 如何动态生成CoreData对象

ios - CMFormatDescriptionRef 和 CMVideoFormatDescriptionGetDimensions

iphone - 这个循环可以优化吗?

iphone - 在后台使用 NSOperationQueue 按时间顺序执行 AFHTTPClient 请求

ios - 收集字符串值并添加到数组中