Special statement: The method used in this article uses the Apple private class Uiimagenibplaceholder, upload AppStore have rejected the risk, only the enterprise version can be ignored this statement
Since IPhone6 and iphone6+ came out, the iOS-developed program Ape has since embarked on the same path as Android, with various adaptations and big heads. In particular, the evil of the iphone6+, is already a very bloated picture plug app now also add a set of @3x pictures, to know that this set of pictures can make the app big one times ah. and @3x picture can be completely compressed into @2x and @1 x pictures, so I resolutely set foot on a picture compatible with all the equipment of the road. After a lot of hardships, touch climbed in the major foreign forums, finally let me find a feasible way.
Don't say much nonsense, first on the code:
Uiimage+localimage.h
#import <UIKit/UIKit.h> @interface UIImage (localimage)/** the original 3 times times the size of the picture to the device corresponding to the size */-(UIImage *) scaledimagefrom3x; @end
&NBSP;&NBSP;&NBSP;&NBSP;UIIMAGE+LOCALIMAGE.M
#import "uiimage+localimage.h" #import <objc/runtime.h>// current IOS version #ifndef __cur_ios_ version#define __cur_ios_version ([[[uidevice currentdevice] systemversion] floatvalue] * 10000) #endif @implementation UIImage (localimage) + (void) load{ if (__CUR_IOS_VERSION&NBSP;>=&NBSP;__IPHONE_8_0) { // because IOS8 is already compatible, you do not need to use the following methods return; } // Replace implementation with code call Imagenamed: when the picture adapts SEL origM = @selector (imagenamed:); sel newm = @selector (imagewithname:); method_exchangeimplementations (Class_ Getclassmethod (SELF,&NBSP;ORIGM), class_getclassmethod (SELF,&NBSP;NEWM)); // This substitution implements the adaptation of the picture in Xib &NBsp; nsstring *classname = [[@ "UIImage" stringbyappendingstring:@ "Nib"] stringbyappendingstring:@ "Placeholder"]; // is written in order to avoid AppStore audit code check, not necessarily effective method m1 = class_getinstancemethod (nsclassfromstring (className), @selector ( Initwithcoder:)); method m2 = class_getinstancemethod (self, @ Selector (initwithcoderfornib:)); method_exchangeimplementations (m1, m2); }/** This method replaces the original imagenamed: Method */+ (uiimage *) Imagewithname: (nsstring * ) name{ uiimage *aimage = [self imagewithname:name]; if (aimage) { // if the corresponding image can be taken, the direct return return aImage; } nsstring *filename = [name stringbyappendingstring:@ "@3x.png"]; aimage = [UIImage imageWithContentsOfFile:[[[NSBundle mainBundle] resourcePath] Stringbyappendingpathcomponent:filename]]; return [aimage &NBSP;SCALEDIMAGEFROM3X];} /** This method replaces the Initwithcoder in the Uiimage-nib-placeholder: because Xib pictures are initialized with this class */- (ID) Initwithcoderfornib: (nscoder *) adecoder{ nsstring *resourcename = [ adecoder decodeobjectforkey:@ "Uiresourcename"]; nsstring *newresourcename = resourceName; if ([resourcename hassuffix:@ ". png"]) { newresourcename = [ resourcename substringtoindex:resourcename.length -4]; } return&nbsP [Uiimage imagenamed:newresourcename];} /** scale the original 3 times-fold image to the device size */- (uiimage *) scaledimagefrom3x{ float locScale = [UIScreen mainScreen].scale; float therate = 1.0 / 3.0; uiimage *newimage = nil; cgsize oldsize = self.size; CGFloat scaledWidth = oldSize.width * theRate; CGFloat scaledHeight = oldSize.height * theRate; CGRect scaledRect = CGRectZero; scaledrect.size.width = scaledwidth; scaledrect.size.height = scaledheight; uigraphicsbeginimagecontextwithOptions (Scaledrect.size, no, locscale); [self drawinrect:scaledrect]; newimage = Uigraphicsgetimagefromcurrentimagecontext (); Uigraphicsendimagecontext (); if (NewImage == nil) { nslog (@ "Could not scale image"); } return newimage;}
How to use: You only need to import these two classes into your project, and you do not require import in other classes. There is no need to modify the original method of loading images, and do not need to modify xib loading pictures, but make sure there is a set of @3x pictures
This method is only used for iOS8 system, because it has been verified that the system above iOS8 has been automatically adapted to the image.
Share your favorite URLs when exploring this method.
1, Google search image (believe that many people have a long time useless google it): Https://github.com/greatfire/wiki
2, Apple private Class (the original Apple has so many things tucked away): Https://github.com/kennytm/iphone-private-frameworks/tree/master
3. Two posts that provide important help:
http://w3facility.org/question/are-images-assigned-in-a-xib-cached/
Http://stackoverflow.com/questions/10016683/in-initwithcoder-what-are-the-keys-in-the-nscoder-a-uinibdecoder-for-uiimag
I hope this method will be helpful to everyone. Reprint please indicate the source OH
iOS set of @3x images compatible with all iOS devices and systems