iOS development 0 lines of code load NSBundle in @2x and @3x pictures

Source: Internet
Author: User

In this paper, we only test, summarize and share the problem of @2x and @3x name that can not be automatically recognized by the Pathforresource of the local picture resource through the method of NSBundle object.


There are two ways to load local picture resources in general:

The 1th type:

    UIImage *img = [UIImage imagenamed:@ "ImageName"];

The 2nd type:

    UIImage *img = [UIImage imagewithcontentsoffile:[[nsbundle mainbundle] pathforresource:@ "ImageName" oftype:@ " ImageType "];


Note: Other methods such as nsdata, such as this article does not involve, if you need to find a brother or a Niang, thank you for sharing.

Suppose we all know that the 1th method is suitable for reading reusable and memory-intensive image resources, and can automatically identify the "@2x" or "@3x" graph according to the current phone hardware. But if you need to load an image resource that is infrequently used and takes up a lot of memory, such as hundreds of kilobytes or even upper m, the memory footprint is bound to be serious. Solve this load image resource consumption memory problem preferred option is to go to the 2nd type, but the resource name passed in must be the same name as ". Suffix", if the resource name added "@2x" or "@3x", and the incoming resource name with or without "@2x" or "@3x" identity, What will the results be like? Let's test it out here.

> without "@2x" or "@3x" logo:

650) this.width=650; "src=" Https://s1.51cto.com/oss/201711/16/8b57e24dc94791653df6e98dc81544e4.png "title=" Img1.png "alt=" 8b57e24dc94791653df6e98dc81544e4.png "/>


> with "@2x" or "@3x" logo


650) this.width=650; "src=" Https://s1.51cto.com/oss/201711/16/d542ba5a5a99c1993c61a676ee9f4640.png "title=" Img2.png "alt=" D542ba5a5a99c1993c61a676ee9f4640.png "/>

It is obvious that the incoming name will be retrieved to the picture resource after it has been identified.


But now I just want to be the 2nd way to load a local image resource can be like the 1th method, do not need to pass in with "@2x" and "@3x" identity can read to the image resources, how do we deal with it?

Method 1: In each place to determine the current device, and ensure that the input file name is correct, that is, the bundle has a resource picture file with or without identification.

    if (is @3x) {reads the        @3x resource picture path;    }    else if (is the @2x graph device) {        reads the @2x resource picture path;    }    else {        Read the path without the @2x and @3x resource picture;    }

But who would like to make this judgment in every place as mentioned above?


Method 2: Add category to NSBundle, enter with or without identification, automatically identify the corresponding resource picture file.

This method is actually the encapsulation of Method 1, the idea with Method 1, but slightly perfect.

The logic is as follows:

    if (is @3x) {        read @3x path,        if (no @3x diagram) {           read @2x resource picture;           {No @2x diagram) {               read without @2x and @3x resource picture;}        }    }    else if (is @2x graph device) {        read @2x graph path,        if (no @2x diagram) {           read @3x resource picture;           {No @3x diagram) {               read without @2x and @3x resource picture;           }        }    }    else {        read without @2x and @3x resource picture,        if (does not exist @1x graph) {           read @2x resource picture;           {No @2x diagram) {               read @3x resource picture;           }        }    }


The code is implemented as follows:

Use runtime knowledge to replace the method in the class method load:

+ (void) load {    Method Originmethod = Class_getinstancemethod (self, @selector (pathforresource:oftype:));    Method Newmethod = Class_getinstancemethod (self, @selector (temppathforresource:oftype:));    Method_exchangeimplementations (Originmethod, Newmethod);}


The

is replaced by the following method:

-(NSString *) Temppathforresource: (NSString *) name OfType: (NSString *) ext {NSString *path = [Self Temppathforresource:    Name Oftype:ext];    if (path) {return path;    } cgfloat scale = [UIScreen mainscreen].scale;        if (ABS (scale-3) <= 0.001) {path = [self temppathforresource_3x:name oftype:ext];            if (!path) {path = [self temppathforresource_2x:name oftype:ext];            if (!path) {path = [self temppathforresource_x:name oftype:ext]; }}} else if (ABS (scale-2) <= 0.001) {path = [self temppathforresource_2x:name oftype:ext]        ;            if (!path) {path = [self temppathforresource_3x:name oftype:ext];            if (!path) {path = [self temppathforresource_x:name oftype:ext];        }}}} else {path = [self temppathforresource_x:name oftype:ext];      if (!path) {path = [self temppathforresource_2x:name oftype:ext];      if (!path) {path = [self temppathforresource_3x:name oftype:ext]; }}} return path;


In this method, the native system is preferred, and if the resource can find the path that returns the resource image, it returns directly; otherwise, the following lookup process is entered. At the end of each search is judged, if the search successfully jumps out if judgment and return to find the path, otherwise go to the next device to find. Among them, look for @2x diagram or @3x graph, judging by this value:

    CGFloat scale = [UIScreen mainscreen].scale;

When judging using variable scale, the "ABS (poor) <= 0.001" method is used, because the property "scale" of the UIScreen object is a value of type cgfloat:

650) this.width=650; "src=" Https://s3.51cto.com/oss/201711/16/50e7f8c3ae909cb8ca819e828f3c41ea.png "title=" Img3.png "alt=" 50e7f8c3ae909cb8ca819e828f3c41ea.png "/>


There is a problem when looking for a resource picture, if the current device is a @3x device, such as IPhone6 plus or IPhone7 plus or other device that needs to @3x the graph resource, but we add the @2x graph resource or @1x graph resource, that is the problem that this article solves.

For devices with different magnification, the logic of processing is not the same.

> Equipment for @1x Charts:

if (the input resource picture is named @3x) {    "@3x" is removed,}else if (the input resource image is named @2x) {    "@2x" is removed;}else {    not processed;} calls the native system method to read path;


> Equipment for @2x Charts:

if (the input resource picture is named @3x) {    Replace "@3x" with "@2x",}else if (input resource picture named @2x) {    not processed;}else {    add "@2x" suffix to the resource picture name} Call the native system method to read path;


> Equipment for @3x Charts:

if (the input resource picture is named @3x) {    No processing,}else if (the input resource picture named @2x) {    "@2x" is replaced with "@3x";}else {    add "@3x" suffix to the resource picture name} Call the native system method to read path;


The code for the above three types of logic is as follows:

> Equipment for @1x Charts:

-(NSString *) temppathforresource_x: (NSString *) name OfType: (NSString *) ext {    NSString *path = nil;    NSString *teampname = nil;        if ([Name hassuffix:@ "@3x"]) {        teampname = [name stringbyreplacingoccurrencesofstring:@ "@3x" withstring:@ "];    }    else if ([name hassuffix:@ "@2x"]) {        teampname = [name stringbyreplacingoccurrencesofstring:@ "@2x" withstring: @""];    }    else {        teampname = name;    }    Path = [self temppathforresource:teampname oftype:ext];        return path;}


> Equipment for @2x Charts:

-(NSString *) temppathforresource_2x: (NSString *) name OfType: (NSString *) ext {    NSString *path = nil;    NSString *teampname = nil;        if ([Name hassuffix:@ "@3x"]) {        teampname = [name stringbyreplacingoccurrencesofstring:@ "@3x" withstring:@ "@2x"];    }    else if ([name hassuffix:@ "@2x"]) {        teampname = name;    }    else {        Teampname = [NSString stringwithformat:@ "%@@2x", name];    }    Path = [self temppathforresource:teampname oftype:ext];        return path;}


> Equipment for @3x Charts:

-(NSString *) temppathforresource_3x: (NSString *) name OfType: (NSString *) ext {    NSString *path = nil;    NSString *teampname = nil;        if ([Name hassuffix:@ "@3x"]) {        teampname = name;    }    else if ([name hassuffix:@ "@2x"]) {        teampname = [name stringbyreplacingoccurrencesofstring:@ "@2x" withstring:@ "@3x "];    }    else {        Teampname = [NSString stringwithformat:@ "%@@3x", name];    }    Path = [self temppathforresource:teampname oftype:ext];        return path;}


After the above processing, the test results are as follows:

650) this.width=650; "src=" Https://s3.51cto.com/oss/201711/16/08fe1f3e78e550907b380e7c857620e5.png "title=" Img4.png "alt=" 08fe1f3e78e550907b380e7c857620e5.png "/>



This article source code see:

Https://github.com/zhoushejun/iOSNotes/blob/master/SJNotes/Classes/UI/Utilities/Categories/NSBundle%2BResource



Resources:

http://blog.csdn.net/null29/article/details/53640179

http://www.jianshu.com/p/f40313d37049


This article from "Jiangshan Wind and Rain" blog, please be sure to keep this source http://965678322.blog.51cto.com/4935622/1982197

iOS development 0 lines of code load NSBundle in @2x and @3x pictures

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.