Как я могу обрезать изображения в cell.imageview.image для UITableViews? - PullRequest
0 голосов
/ 10 марта 2012

Я читал об этом здесь , здесь и здесь - но не смог понять это.У меня есть UITableView, который отображает миниатюру изображения (шириной 66 пикселей), но когда он отображается в таблице, изображения не выстраиваются в ряд, что отбрасывает текст и заставляет вещи выглядеть неполированными:

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 голосов
/ 10 марта 2012

Ограничение размера 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];
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...