iOS is automatically adapted to the image name (implemented by category)

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.