[UIImage imageNamed…] vs [UIImage imageWithData…]
2011-07-21 21:53
225 查看
问:
I want to load some images into my application from the file system. There's 2 easy ways to do this:
回答:
It depends on what you're doing with the image. The
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.
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简谈:[UIImage imageNamed:]与 [UIImage imageWithContentsOfFile]的区别
- UIImage imageNamed 与 imageWithContentsOfFile的差别
- UIImage imageNamed 与 imageWithContentsOfFile的区别
- imageNamed/ imageWithContentsOfFile /imageWithData 的区别
- imageNamed vs imageWithContentsOfFile
- imageNamed/ imageWithContentsOfFile /imageWithData 的区别
- imageNamed/ imageWithContentsOfFile /imageWithData 的区别
- iOS_UIImage中 + imageNamed: 和 + imageWithContentsOfFile:两个方法的区别
- [UIImage resizableImageWithCapInsets:]使用注意
- IOS 笔记 - 利用UIImage的stretchableImageWithLeftCapWidth方法给UIButton设置背景
- UIImage部分拉伸——stretchableImageWithLeftCapWidth的使用
- 【原】[UIImage imageWithContentsOfFile:]引发的图片无法显示的问题
- [UIImage imageWithCVMat:(const cv::Mat&)cvMatt] 内存问题
- WJ的Direct3D简明教程3:Create Texture with User-defined Image Data
- Check failed: registry.count(type) == 1 (0 vs. 1) Unknown layer type: ImageData
- 关于uiimage imagenamed内存可能占用大的替代方法
- [转]Import List Instances and their data with Mavention Import List Instance(使用VS插件导出列表数据定义)
- [UIImage imageWithContentsOfFile:@""] 内存警告
- imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal让item上的图片原样显示
- IOS UIImage图片拉伸技巧-resizableImageWithCapInsets