我想从文件系统将一些图像加载到我的应用程序中.有两种简单的方法可以做到这一点:
[UIImage imageNamed:fullFileName]
Run Code Online (Sandbox Code Playgroud)
要么:
NSString *fileLocation = [[NSBundle mainBundle] pathForResource:fileName ofType:extension];
NSData *imageData = [NSData dataWithContentsOfFile:fileLocation];
[UIImage imageWithData:imageData];
Run Code Online (Sandbox Code Playgroud)
我更喜欢第一个,因为它的代码少得多,但是我看到有些人说图像被缓存了,而且这个方法使用了更多的内存?由于我不相信大多数其他论坛上的人,我想我会在这里问这个问题,是否有任何实际的区别,如果是的话哪个更"好"?
我尝试使用对象分配工具分析我的应用程序,我看不出任何实际差异,虽然我只在模拟器中尝试过,而不是在iPhone本身上.
ObjectiveC中b/w imageNamed和imageWithContentsOfFile有什么区别