In the actual development of the application, the art will usually be based on different models to design a lot of background images, and these background images are often named for the regular, we can just use this feature to change the name of the image or splicing into a method, and then according to different models to return different adaptation pictures, Can achieve the purpose of automatic adaptation, and to avoid the distortion of the picture.
First, create a new category for NSString, which is specifically used to splice the corresponding screen information behind the image name.
#import "Nsstring+append.h" @implementation nsstring (Append)-(NSString *) Filenameappend: (NSString *) string{ // Get the extension nsstring *extension = [self pathextension]; Remove extension nsstring *filename = [self stringbydeletingpathextension]; concatenation string name filename = [filename stringbyappendingstring:string]; Add extension nsstring *newfilename = [FileName stringbyappendingpathextension:extension]; Returns the processed picture name return NewFileName;} @end
Next, write a classification for uiimage, the function is to pass in the name of the picture, return to my corresponding model of the adaptation picture
#import "Uiimage+adjustimage.h" #import "Nsstring+append.h" @implementation UIImage (Adjustimage)-(UIImage *) Addimagenameforfit: (NSString *) name{ //Make judgments, add different image name suffixes to different models, return different adaptation picture if (iPhone5) { name = [name] filenameappend:@ "[email protected]"; } else if (iPhone6) { name = [name filenameappend:@ "[email protected]"]; } else if (iphone6plus) { name = [name] filenameappend:@ "[email protected]"; } return [UIImage imagenamed:name];} @end
So, through these two categories (classification), if I want to load a background picture, no need to consider the model adaptation problem, as long as the incoming background.jpg, the program will be based on the model is IPhone5, IPhone6 or iphone6plus automatically add the corresponding suffix to the image name, and then to the project to find the corresponding picture loaded into the screen, so as to ensure that the picture is not true and not be stretched
iOS is automatically adapted to the image name (implemented by category)