Cause of the problem
I’m sure a lot of you who are new to iOS will get a nil return when you use the pathForResource method. Me too. I had this problem some time ago. I did a little research on this method to find out, and here’s what I learned.
-
The most common problem is that when you import a resource map, you don’t hit addTarget, you just copy it into the project, it doesn’t belong to the project, so pathForResource doesn’t get the image and returns nil. I had this problem last time. I didn’t pay attention when importing, and then I felt no problem finding it.
-
Images (files) placed in images.xcassets cannot be retrieved using pathForResource. They cannot be retrieved, but can only be retrieved using the [UIImage imageNamed:] method.
-
So pathForResource is going to add at sign 2x, at sign 3x, not add it and just find the 1x. The reason is that this method does an exact lookup based on the name and type you provide, rather than an adaptive multiplier like the imageNamed: method.
Here are the highlights (solutions)
As we all know, pathForResource loads images much faster than imageNamed and does not cache them in memory. Using The pathForResource method for infrequently used images is more efficient and memory efficient than imageNamed. Of course, if you’re going to use an image a lot, it’s better to use imageNamed:.
If we want to use multiples and want to use pathForResource to load images, what should we do? Without further ado, the above code (attached with the model adaptation code, need to use) :
Model adaptation code
// // adaptivedefine. h // iOS adapter test // // Created by chy on 2018/12/20. // Copyright © 2019 chy. All Rights Reserved. //#ifndef AdaptiveDefine_h
#define AdaptiveDefine_h// Status bar height#define Height_StatusBar ((is_iPhoneXs == YES || is_iPhoneXr == YES || is_iPhoneXsMax == YES) ? 44.0:20.0)// Height of navigation bar#define Height_NavBar ((is_iPhoneXs == YES || is_iPhoneXr == YES || is_iPhoneXsMax == YES) ? 88.0:64.0)/ / tabBar height#define Height_TabBar ((is_iPhoneXs == YES || is_iPhoneXr == YES || is_iPhoneXsMax == YES) ? 83.0:49.0)// iPhone5 series 5,5s#define is_iPhone5 ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(640, 1136), [[UIScreen mainScreen] currentMode].size) : NO)IPhone6 series 6 6,7,8,6s#define is_iPhone6 ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(750, 1334), [[UIScreen mainScreen] currentMode].size) : NO)Iphone6 + series 6plus, 7Plus, 8Plus, 6sPlus#define is_iPhone6Plus ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(1242, 2208), [[UIScreen mainScreen] currentMode].size) : NO)// check iPHoneXr Xr#define is_iPhoneXr ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(828, 1792), [[UIScreen mainScreen] currentMode].size) : NO)// iPhoneX series X,Xs#define is_iPhoneXs ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(1125, 2436), [[UIScreen mainScreen] currentMode].size) : NO)IPhoneXs Max Xs Max#define is_iPhoneXsMax ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(1242, 2688), [[UIScreen mainScreen] currentMode].size) : NO)
#endif /* AdaptiveDefine_h */
Copy the code
Loading images:
// Use imageWithContentOfFile to find PNG images#define UIImageFilePNG(fileName) [UIImage imageWithContentsOfFile: [[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"%@%@", fileName, (is_iPhone5 || is_iPhone6 || is_iPhoneXr) ? @"@2x" : @"@3x"] ofType:@"png"]]// When loading other resources#define UISourceFile(fileName, fileType) [UIImage imageWithContentsOfFile: [[NSBundle mainBundle] pathForResource:fileName ofType:fileType]]
Copy the code
Attached is the resolution and usage magnification of iOS models (1x magnification is basically not used anymore)
See this believe that many partners have understood the principle of implementation, is not very simple ah! The first nuggets, write may not be very good, if can help you is the best encouragement to me, do not understand or have objections to the place can also react with me at any time!