调整TableView上的图像大小

时间:2013-11-11 05:44:48

标签: ios objective-c uitableview

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

    // Set the data for this cell:

        cell.textLabel.text = [namesArray objectAtIndex:indexPath.row];
        cell.textLabel.textColor = [UIColor grayColor];
        cell.textLabel.font = [UIFont fontWithName:@"Trebuchet MS" size:14];
        cell.textLabel.textAlignment = UITextAlignmentLeft;

        cell.imageView.image = [UIImage imageNamed:[imagesArray objectAtIndex:indexPath.row ]];
        UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 5, 5)];
        imgView.image = [UIImage imageNamed:[imagesArray objectAtIndex:indexPath.row ]];
        cell.imageView.image = imgView.image;

        [cell.imageView setFrame:CGRectMake(0, 0, 55, 55)];

        cell.selectionStyle = UITableViewCellSelectionStyleGray;

       return cell;
}

此处我仅使用TableView而不是UITableViewCell,但我想调整TableView上的图片大小。我在互联网上看了很多例子,但是我不明白它们是如何工作的 - 我无法在TableView上调整它们的大小。有人可以解释如何让它工作? [cell.imageView setFrame:CGRectMake(0,0,55,55)];不工作

3 个答案:

答案 0 :(得分:1)

这里我将裁剪和调整大小图像的逻辑,根据您的要求使用它。

获取裁剪图片:

UIImage *croppedImg = nil;
CGRect cropRect = CGRectMake(AS YOu Need); // set frame as you need
croppedImg = [self croppIngimageByImageName:self.imageView.image toRect:cropRect];

以下方法返回UIImage,如您所希望的图像大小

- (UIImage *)croppIngimageByImageName:(UIImage *)imageToCrop toRect:(CGRect)rect
    {
        //CGRect CropRect = CGRectMake(rect.origin.x, rect.origin.y, rect.size.width, rect.size.height+15);

        CGImageRef imageRef = CGImageCreateWithImageInRect([imageToCrop CGImage], rect);
        UIImage *cropped = [UIImage imageWithCGImage:imageRef];
        CGImageRelease(imageRef);

        return cropped;
    }

在这里你得到通过上述方法返回的Croped Image;

或重新调整

并且还使用以下方法将图像的特定宽度用于调整大小:

+ (UIImage*)resizeImage:(UIImage*)image withWidth:(CGFloat)width withHeight:(CGFloat)height
{
    CGSize newSize = CGSizeMake(width, height);
    CGFloat widthRatio = newSize.width/image.size.width;
    CGFloat 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 :(得分:1)

如果要自定义单元格的方面(例如图像视图的大小),则需要使用自定义UITableViewCell。目前您正在使用标准单元格UITableViewCellStyleDefault,它不允许您自定义图像视图的大小。

在情节提要或Xib文件中创建UITableViewCell,将其添加到图片视图中并将其标记设置为数字。之后,您需要检索单元格以进行配置:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"CellIdentifier";
    UITableViewCell *cell = (UITableViewCell *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

    UIImageView *imageView = (UIImageView *) [cell viewWithTag:TAG_NUMBER];
    // do your stuff

    return cell;
}

答案 2 :(得分:0)

也许你可以做这样的事情。只需在单元格的contentView下添加一个新的imageView,您将更容易设置框架。

只是为了澄清你已经在使用UITableviewCell了。

- (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];
// Create a new imageview to have your desired frame

  UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 5, 5)];

 // Add tag 
  imgView.tag = 100;

// Add the imageView to the contentView of the cell
 [cell.contentView addSubview:imgView];

}

// Other codes


UIImageView *imageV = (UIImageView *)[cell.contentView viewWithTag:100];
// Assign Image here
imageV.image = [UIImage imageNamed:[imagesArray objectAtIndex:indexPath.row ]];


cell.selectionStyle = UITableViewCellSelectionStyleGray;

   return cell;
}
相关问题