Я читал об этом здесь , здесь и здесь - но не смог понять это.У меня есть UITableView, который отображает миниатюру изображения (шириной 66 пикселей), но когда он отображается в таблице, изображения не выстраиваются в ряд, что отбрасывает текст и заставляет вещи выглядеть неполированными:
Вот моя попытка ограничить его этими измерениями в 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;
}