IOS only uses the 3x image solution (source code included), ios3x
With the introduction of 6 plus by Apple, image resources have become a major problem. 2x-640x960 2x-750x1334 3x-1242x2208. The installation packages of various types of images will inevitably increase a lot, so many types of images will inevitably be missed, therefore, you have written a Category library for UIImage. You only need to add a 3x image to the Bundle to use the 3x image for 6 plus and 2x for 6 or later versions.
For more information, see the code and usage.
(If you have any questions, please reply and rest)
/// UIImage + Compress. h // ImageCompress /// Created by Hunk on 15/3/3. // Copyright (c) 2015 Hunk. all rights reserved. /// image compression. Only images of @ 3x.png exist in the bundle. Images of @ 2x.png are automatically generated to Sandbox as needed # import <UIKit/UIKit. h>/* usage ** logo_huodong: The image name suffixed with "@ 3x.png" in bundle. you can directly enter the image name * imageWithName: the Category Method for UIImage ** UIImage * image = [UIImage imageWithName: @ "logo_huodong"]; * UIImageView * imageView = [[UIImageView alloc] initWithFrame: CGRectMake (CGRectGetWidth (self. view. bounds)-image. size. width)/2.0, 50.0, image. size. width, image. size. height)]; * [imageView setImage: image]; * [self. view addSubview: imageView]; */@ interface UIImage (Compress)/*** Get image with image name ** @ param name: Image name. A image named "logo@3x.png", inputing "logo" is necessary. ** @ return UIImage */+ (UIImage *) imageWithName :( NSString *) name; @ end
/// UIImage + Compress. m // ImageCompress /// Created by Hunk on 15/3/3. // Copyright (c) 2015 Hunk. all rights reserved. // # import "UIImage + Compress. h "# define SUFFIX_3X_PNG @" @3x.png "# define SUFFIX_2X_PNG @" @2x.png "@ implementation UIImage (Compress) // obtain image + (UIImage *) imageWithName :( NSString *) name {if ([[[UIDevice currentDevice] systemVersion] floatValue]> = 8.0) {return [UIImage imageNamed: name];} Else {NSAssert (NO = [name hasSuffix: SUFFIX_3X_PNG], @ "File name suffix @3x.png is unnecessary! "); NSString * srcImgName = [name stringByAppendingString: SUFFIX_3X_PNG]; NSString * desImgName = [UIImage desImageName: [UIImage getImageName: name] withExt: SUFFIX_2X_PNG]; // Path NSString * cacheDir = [NSHomeDirectory () stringByAppendingPathComponent: @ "/Library/Caches/Resources/images/"]; // you can check whether the folder where the image is stored exists, if the folder does not exist, create NSFileManager * fileManager = [NSFileManager defamanager manager]; BOOL isDir = NO; BOOL IsDirExist = [fileManager fileExistsAtPath: cacheDir isDirectory: & isDir]; if (! (IsDirExist & isDir) {NSError * error = nil; BOOL bCreateDir = [fileManager createDirectoryAtPath: cacheDir withIntermediateDirectories: YES attributes: nil error: & error]; if (! BCreateDir) {NSLog (@ "Create Directory Failed!: % @ ", Error. description); return nil ;}nsstring * desPath = [cacheDir stringByAppendingPathComponent: desImgName]; UIImage * desImage = [UIImage imageWithContentsOfFile: desPath]; if (! DesImage) {// if the image is not obtained from the Cache if ([UIImage compressImage: srcImgName compressionQuality: 1.0 desPath: desPath]) {// The compression is successful. desImage = [UIImage imageWithContentsOfFile: desPath];} else {desImage = nil;} return desImage;} // compressed image + (BOOL) compressImage :( NSString *) name compressionQuality :( CGFloat) compressionQuality desPath :( NSString *) desPath {if (name = nil | desPath = nil) {return NO;} UIImage * SrcImage = [UIImage imageNamed: name]; if (srcImage = nil) {return NO;} // set the image size UIScreen * mainScreen = [UIScreen mainScreen] based on the screen size; CGFloat imageScale = srcImage. scale * 2.0/3.0; CGSize imageSize = CGSizeMake (srcImage. size. width * imageScale, srcImage. size. height * imageScale); CGSize screenSize = CGSizeMake (CGRectGetWidth (mainScreen. bounds) * mainScreen. scale), (CGRectGetHeight (mainScreen. Bounds) * mainScreen. scale); if (imageSize. width> imageSize. height) {// Width> Height if (imageSize. width> screenSize. width) {imageSize. height = (screenSize. width * imageSize. height/imageSize. width); imageSize. width = screenSize. width ;}} else {// Height> Width if (imageSize. height> screenSize. height) {imageSize. width = screenSize. height * (imageSize. width/imageSize. height); imageSize. Height = screenSize. height;} // Reset image UIImage * newImage = [UIImage imageWithImage: srcImage scaleToSize: imageSize]; // compress the image NSData * imageData = compressionQuality> = 1? UIImagePNGRepresentation (newImage): UIImageJPEGRepresentation (newImage, compressionQuality); // Save the new image return [imageData writeToFile: desPath atomically: YES];} // reset the full name (with extension) of the image) + (NSString *) desImageName :( NSString *) srcImageName withExt :( NSString *) extName {return [srcImageName stringByAppendingString: extName];} // obtain the image name (without the extension) + (NSString *) getImageName :( NSString *) srcImage {if (srcImage) {NSArray * tempArray = [srcImage componentsSeparatedByString :@". "]; if (tempArray) {// yes. split file name if ([tempArray count]> 1) {NSString * extName = [tempArray lastObject]; // strictly judge the object extension if ([extName is1_tostring: @ "png"] | [extName isname tostring: @ "jpg"] | [extName isname tostring: @ "jpeg"]) {return [srcImage substringWithRange: NSMakeRange (0, srcImage. length-(extName. length + 1)];} else {return nil ;}} else {return srcImage ;}} return nil ;} // reset the image size + (UIImage *) imageWithImage :( UIImage *) image scaleToSize :( CGSize) newSize {UIGraphicsBeginImageContext (newSize); [image drawInRect: CGRectMake (0.0, 0.0, newSize. width, newSize. height)]; UIImage * newImage = UIGraphicsGetImageFromCurrentImageContext (); UIGraphicsEndImageContext (); return newImage;} @ end