如何创建一个UIImageView对象去显示图片
2015-11-25 20:06
423 查看
方法1:
UIImage *image = [UIImage imageNamed:@"huahuo.png"];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
imageView.frame = CGRectMake(10, 50, self.frame.size.width - 20, 100);
[self addSubview:imageView];
[imageView release];
方法2
NSString *path = [NSBundle mainBundle] pathForResource:@"huahuo" ofType:@".png"];
UIImage *image = [UIImage alloc] initWithContentsOfFile:path] ;
UIImageView *imageView = [[UIImaneView alloc] initWithImage:image];
imageView.frame = CGrectMake(10, 50, self.frame, size.width - 20, 100);
[self addSubview:imageView];
[imageView release];
两种方法的不同点:
方法一:用imageName加载图片的时候,实质上系统会把图片缓存到内存中去,如果图片较大或者图片较多的情况下用这张方法会消耗很大的内存,而且对于图片的内存释放也是一件非常复杂的事情,而对于同一张图片,只会缓存一次,对于重复利用非常有优势
方法二:initWithContentOfFile这个方法就是通过查找图片在工程中的位置,然后去加载这张图片,图片数据不会被缓存,对于图片较大,而且使用情况较少的情况下,使用此方法可以节省内存消耗
UIImage *image = [UIImage imageNamed:@"huahuo.png"];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
imageView.frame = CGRectMake(10, 50, self.frame.size.width - 20, 100);
[self addSubview:imageView];
[imageView release];
方法2
NSString *path = [NSBundle mainBundle] pathForResource:@"huahuo" ofType:@".png"];
UIImage *image = [UIImage alloc] initWithContentsOfFile:path] ;
UIImageView *imageView = [[UIImaneView alloc] initWithImage:image];
imageView.frame = CGrectMake(10, 50, self.frame, size.width - 20, 100);
[self addSubview:imageView];
[imageView release];
两种方法的不同点:
方法一:用imageName加载图片的时候,实质上系统会把图片缓存到内存中去,如果图片较大或者图片较多的情况下用这张方法会消耗很大的内存,而且对于图片的内存释放也是一件非常复杂的事情,而对于同一张图片,只会缓存一次,对于重复利用非常有优势
方法二:initWithContentOfFile这个方法就是通过查找图片在工程中的位置,然后去加载这张图片,图片数据不会被缓存,对于图片较大,而且使用情况较少的情况下,使用此方法可以节省内存消耗
相关文章推荐
- UITableView——reloadData与reloadSection性能比较
- 053-19 What recommendations does the SQL Access Advisor provide for optimizing SQL queries? (Choose
- CSS media queries
- hdu 4740 The Donkey of Gui Zhou(dfs模拟好题)
- UINavigationBar字体颜色
- 使用Autolayout实现UITableView的Cell动态布局和高度动态改变
- iOS开发小技巧:刷新UITableView
- Ant的build.xml脚本模板
- 053-18 What two statements are true regarding the recommendations received from the SQL Access Advi
- loadrunner :Insufficient records for parameter in table to provide the Vuser with unique data
- StringBuffer和StringBuilder的区别
- 【Codeforces Round 333 (Div 2)D】【线段树 or ST-RMQ 初始化78msAC】Lipshitz Sequence 若干区间询问所有子区间的答案和
- 读书笔记--UI组件
- 【bzoj1819】[JSOI]Word Query电子字典 trie+暴力
- UI第十二天:通讯录实战
- LightOJ 1048 - Conquering Keokradong(二分)
- 自定义UIButton
- Request.Item和Request.Params
- hdu5568 sequence2(dp+大数)
- BIOS锁定纯UEFI启动的解锁办法