[UIImage imageNamed…] vs [UIImage imageWithData…]
来源:互联网 发布:红宝书软件下载 编辑:程序博客网 时间:2024/06/06 17:33
问:
I want to load some images into my application from the file system. There's 2 easy ways to do this:
[UIImage imageNamed:fullFileName]
or:
NSString *fileLocation = [[NSBundle mainBundle] pathForResource:fileName ofType:extension];NSData *imageData = [NSData dataWithContentsOfFile:fileLocation];[UIImage imageWithData:imageData];
I prefer the first one because it's a lot less code, but I have seen some people saying that the image is cached and that this method uses more memory? Since I don't trust people on most other forums, I thought I'd ask the question here, is there any practical difference, and if so which one is 'better'?
I have tried profiling my app using the Object Allocation instrument, and I can't see any practical difference, though I have only tried in the simulator, and not on an iPhone itself.
回答:
It depends on what you're doing with the image. The imageNamed:
method does cache the image, but in many cases that's going to help with memory use. For example, if you load an image 10 times to display along with some text in a table view, UIImage will only keep a single representation of that image in memory instead of allocating 10 separate objects. On the other hand, if you have a very large image and you're not re-using it, you might want to load the image from a data object to make sure it's removed from memory when you're done.
If you don't have any huge images, I wouldn't worry about it. Unless you see a problem (and kudos for checking Object Allocation instead of preemptively optimizing), I would choose less lines of code over negligible memory improvements.
- [UIImage imageNamed…] vs [UIImage imageWithData…]
- (UIImage *)imageNamed
- (UIImage *)imageNamed
- UIImage imageNamed 内存问题!!!
- UIImage imagenamed 用法好坏
- [UIImage imageNamed:@""]使用误区
- [UIImage imageNamed:@""]使用误区
- [UIImage imageNamed:@""]使用误区
- [UIImage imageNamed:@""]使用误区
- [UIImage imageNamed:@""]使用误区
- UIImage imageNamed格式要求
- UIImage简谈:[UIImage imageNamed:]与 [UIImage imageWithContentsOfFile]的区别
- ios开发UIImage imageNamed方法
- UIImage imageNamed的释放问题
- uiimage
- UIImage
- UIImage
- UIImage
- (iphone) UIImage memory management problem.
- APDU命令
- linux下设置定时任务-cron使用
- Boost学习系列2-智能指针(下)
- 粘包问题
- [UIImage imageNamed…] vs [UIImage imageWithData…]
- 使用T-SQL连接另一SQL实例,读取数据表(备忘)
- 使用Vim打造IDE--高亮显示
- POJ 1328 Radar Installation
- DB2死锁处理
- SELECT FILE APDU
- socket、poll、epoll的区别
- Android menu 使用初步
- java duck的屏幕保护程序