UIImage初始化两种方式的比较

C的两种方式:

UIImage *image1 = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"image1.jpg" ofType:nil]];

UIImage *image1 = [UIImage imageNamed:@"image1.jpg"];

这两种方式有什么区别?什么时候该使用哪一种方式,跟效率有关么?

UIImage imageNamed
If this is the first time the image is being loaded, the method looks for an image with the specified name in the application’s main bundle.
This method looks in the system caches for an image object with the specified name and returns that object if it exists. If a matching image object is not already in the cache, this method loads the image data from the specified file, caches it, and then returns the resulting object.
会去寻找缓存,如果找不到从NSBundle中找到,缓存,返回对象。找不到返回nil.

UIImage imageWithContentsOfFile
Creates and returns an image object by loading the image data from the file at the specified path.
真接找路径中的图片,找不到返回nil.

猜你喜欢

转载自lizhuang.iteye.com/blog/1676643