在我的项目中,很多地方都希望将UITableViewCell 中的imageView 能根据自己图片的大小来进行展示,而就为了解决这个问题又觉得重写UITableViewCell 很不值得。

如下:

其实我就只有 图片变形的问题。所以我第一时间想到的是调整imageView 内容布局模式以及外框大小

cell.imageView.contentMode=UIViewContentModeCenter;

cell.imageView.frame=....

另外如:

cell.imageView.autoresizingMask = ( UIViewAutoresizingNone );
cell.imageView.autoresizesSubviews = NO;
cell.imageView.contentMode = UIViewContentModeCenter;
cell.imageView.bounds = CGRectMake(0, 0, 50, 50);
cell.imageView.frame = CGRectMake(0, 0, 50, 50);
cell.imageView.image = [UIImage imageWithData: imageData];

但是这些方法 在UITableViewCell里面都不起作用。。。。

在网上找了老半天也没有找到,后来还是在stackoverflow 里面找到了一种自己比较喜欢的解决方案,如下:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
[...]
        
//1、首先对image付值
cell.imageView.image=[UIImage imageNamed:@"灰时间"];
//2、调整大小
      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(); [...]
return cell;
}

结果如下:

很开心。。。完美解决

最新文章

  1. Ubuntu(Linux系统)虚拟机工具vmtools详细说明
  2. Windows Live Writer 的昨日荣光
  3. 网络编程之socket
  4. 解决sublime3 package control显示There are no packages available for installation
  5. Unity3d《Shader篇》变胖
  6. Qt Designer 修改窗体大小改变控件位置
  7. nodejs框架express实现登录
  8. mongodb+pycharm使用报错,无法往mongodb存文件
  9. iOS之UI--Quartz2D的入门应用--重绘下载圆形进度条
  10. dateTimePicker的使用,时间控件
  11. SpringMVC4.2.4 xml配置
  12. MySQL的YEARWEEK函数(转)
  13. Makefile学习(三)执行make
  14. 基本的Logstash 例子
  15. STSR round#1
  16. 一篇文章彻底搞懂es6 Promise
  17. 【深度学习系列】卷积神经网络CNN原理详解(一)——基本原理
  18. python 多个脚本
  19. 关于php开发中的字符编码问题总结的几个要点
  20. iBatis.Net 配置 SQL语句执行 日志

热门文章

  1. TextView,EditText中添加不同颜色的文字
  2. python: HTML中的选择器
  3. 图论$\cdot$最短路问题
  4. iOS开发中的内存分配(堆和栈)
  5. hdu 3966 Aragorn's Story 树链剖分 按点
  6. ggplot2.multiplot:将多个图形使用GGPLOT2在同一页上
  7. 细话 - 如何在web应用中使用百度地图
  8. [Python正则表达式] 字符串中xml标签的匹配
  9. Android Activity的加载模式和onActivityResult方法之间的冲突
  10. redis 库相关命令