如何在cell.imageview.image中为UITableViews裁剪图像?

时间:2012-03-10 04:01:28

标签: ios cocoa-touch uitableview uiimageview

我已经了解了这个hereherehere - 但未能弄明白。 我有一个显示图像缩略图(宽度为66px)的UITableView,但是当它显示在表格中时,图像不能很好地排列,这会使文本关闭并使事物看起来不亮:

UITableView with images out of alignment

这是我尝试将其限制在cellForRowAtIndexPath中的这些维度:

UIImage *largeImage = [UIImage imageWithContentsOfFile: uniquePath];
CGRect cropRect = CGRectMake(0, 0, 66, 49); 

CGImageRef imageRef = CGImageCreateWithImageInRect([largeImage CGImage], cropRect);
cell.imageView.image = [UIImage imageWithCGImage: imageRef];

但似乎没有任何区别。 如果这是最简单的方法,我不介意在添加缩略图时裁剪缩略图文件,但它需要在iPhone和iPad上运行。

什么是最简单的解决方案?

CellForRow方法:

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

 //Configure the cell...
Story *storyAtCell = [fetchedResultsController objectAtIndexPath:indexPath];

//this should be the sentence object in sentences with an order of 0
NSPredicate *predicateTitle = [NSPredicate predicateWithFormat:@"order=0"];
NSSet *sentences = [[storyAtCell sentences] filteredSetUsingPredicate:predicateTitle];
Sentence *sentenceAtCell = [sentences anyObject];

cell.textLabel.text = sentenceAtCell.text;

NSArray *paths       = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
NSString *uniquePath = [[paths objectAtIndex:0] stringByAppendingPathComponent:[sentenceAtCell thumb]];


// This should crop it as you want - you've just got to create cropRect.

UIImage *largeImage = [UIImage imageWithContentsOfFile: uniquePath];


//CGRect cropRect = CGRectMake(0, 0, 66, 50); 
//CGImageRef imageRef = CGImageCreateWithImageInRect([largeImage CGImage], cropRect);

//Discussion here: https://stackoverflow.com/questions/9643818/how-can-i-crop-images-in-cell-imageview-image-for-uitableviews

UIImageView *cellimage = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0 , 66, 50)];
UILabel *labelFrame = [[UILabel alloc] initWithFrame:CGRectMake(0, 0 , 70, 50)];

[cell.contentView addSubview:cellimage];
[cell.contentView addSubview:labelFrame];
[cellimage setImage:largeImage];


//cell.imageView.image = [UIImage imageWithCGImage: cellimage];
//CGImageRelease(imageRef);
[cellimage release];
[labelFrame release];

cell.accessoryType =  UITableViewCellAccessoryDisclosureIndicator;

return cell;
}

1 个答案:

答案 0 :(得分:0)

限制ImageView大小对我很有用:

UIImageView *cellimage = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0 , 107, 70)];
    [cell.contentView addSubview:cellimage];
    [cellimage setImageWithURL:[self.Featured_ImageURLs objectAtIndex:indexPath.row - 1]    
                   placeholderImage:[UIImage imageNamed:@"small_news_default.png"] View:@"Highlights"]; // you have to change this based on your code.
    [cellimage release];