2012-03-10 5 views
0

私はこれについてhere,herehereについて読んだことがありますが、それを理解することはできませんでした。 私は(66pxの幅)の画像のサムネイルを表示するのUITableViewを持っていますが、それは表に表示されますときの画像はきれいに並ばないテキストをオフにスローし、物事が玄米に見えるた:cell.imageview.imageでUITableViewsの画像をトリミングするにはどうすればよいですか?

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; 
} 
+0

私の答えにあなたの答えのための – Shubhank

答えて

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]; 
+0

おかげで私の最後のコメントをチェック - イメージにぴったりのサイズになり、しかし、彼らは現在、私のセルテキストを重複/クリッピングしています - それをどうやって修正しますか? – glenstorey

+0

107(画像ビューの幅)以上のフレームを持つUilabelを作成し、それをcontentViewに追加します。 – Shubhank

+0

UILabel * labelFrame = [[UILabel alloc] initWithFrame:CGRectMake(0、0、70、50)];ホワイトボックスのためにイメージをスワップアウトし、テキストを移動しません。 (私の画像ビュー幅は66pxです)。 – glenstorey

関連する問題