iOS UITableViewCell 中 调整imageView 的图片大小
2024-08-24 03:58:16
在我的项目中,很多地方都希望将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;
}
结果如下:
很开心。。。完美解决
最新文章
- Ubuntu(Linux系统)虚拟机工具vmtools详细说明
- Windows Live Writer 的昨日荣光
- 网络编程之socket
- 解决sublime3 package control显示There are no packages available for installation
- Unity3d《Shader篇》变胖
- Qt Designer 修改窗体大小改变控件位置
- nodejs框架express实现登录
- mongodb+pycharm使用报错,无法往mongodb存文件
- iOS之UI--Quartz2D的入门应用--重绘下载圆形进度条
- dateTimePicker的使用,时间控件
- SpringMVC4.2.4 xml配置
- MySQL的YEARWEEK函数(转)
- Makefile学习(三)执行make
- 基本的Logstash 例子
- STSR round#1
- 一篇文章彻底搞懂es6 Promise
- 【深度学习系列】卷积神经网络CNN原理详解(一)——基本原理
- python 多个脚本
- 关于php开发中的字符编码问题总结的几个要点
- iBatis.Net 配置 SQL语句执行 日志
热门文章
- TextView,EditText中添加不同颜色的文字
- python: HTML中的选择器
- 图论$\cdot$最短路问题
- iOS开发中的内存分配(堆和栈)
- hdu 3966 Aragorn's Story 树链剖分 按点
- ggplot2.multiplot:将多个图形使用GGPLOT2在同一页上
- 细话 - 如何在web应用中使用百度地图
- [Python正则表达式] 字符串中xml标签的匹配
- Android Activity的加载模式和onActivityResult方法之间的冲突
- redis 库相关命令