我一直在使用cellforrowatindexpath中的以下代码的变体来为tableview制作缩略图。但是,我相信这段代码不必要地复杂并且不正确地拉伸一些图像。任何人都可以建议正确的方式(即有效,一致和非扭曲)制作一个圆形缩略图(大约40x40)在桌面视图中显示?感谢信:
//poor code
CGSize itemSize = CGSizeMake(40, 40);
UIGraphicsBeginImageContextWithOptions(itemSize, NO, UIScreen.mainScreen.scale);
CGRect imageRect = CGRectMake(0.0, 0.0, itemSize.width, itemSize.height);
[cell.imageView.image drawInRect:imageRect];
cell.imageView.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
cell.imageView.layer.masksToBounds=YES;
cell.imageView.layer.cornerRadius=20.0;
cell.imageView.frame = CGRectMake(20, 20, 500, 50);
//add image to right
return cell;
注意:此answer中的yah方法在详细视图中适用于我,但在tableview控制器单元格中不适用
//does not work consistently. Sometimes images are square, sometimes round, sometimes football shaped
cell.imageView.layer.cornerRadius = cell.imageView.frame.size.width / 2;
cell.imageView.contentMode = UIViewContentModeScaleAspectFill;
cell.imageView.clipsToBounds = YES;