3x Image Solution for IOS only (with source)

Source: Internet
Author: User

With Apple launched 6plus, picture resources become a big problem, 2x-640x960 2x-750x1334 3x-1242x2208, various specifications of the picture, installation package will inevitably increase a lot, so many kinds of pictures would inevitably miss out some, So I wrote a uiimage category library, only need to add 3x images in the bundle to achieve 6plus use 3x images, 6 and the following version of the use of 2x pictures.

Don't talk much, see the code and how to use it.

(If you have any questions, please respond directly, rest)

  uiimage+compress.h//  imagecompress////  Created by Hunk on 15/3/3.//  Copyright (c) 2015 Hunk. All rights reserved.////  image compression, bundle only need to exist @3x.png pictures, @2x.png pictures will be automatically generated into the sandbox as needed #import <uikit/uikit.h >/* How to use * * logo_huodong     : Bundle suffix is @3x.png name of the picture, use without any suffix, directly into the name can be * 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 "[Email protected]", 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 BASE_IMG_WIDTH (1242) #define Base_img_height (2208) #define Suffix_3x_png @ "@3x.png" #define Suffix_2x_png @ "@2x.png" @implementation UIImage (Compress)//Get picture + (UIImage *) Imagewit  Hname: (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/"                ]; Determine if the folder where the picture is stored exists and does not exist create the corresponding folder NsfilemanaGer *filemanager = [Nsfilemanager Defaultmanager];        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 picture is not taken from the cache ([UIImage compressimage:srcimgname compressionquality:0. 8 Despath:despath]) {//compression succeeded desimage = [UIImage ImagewithcontentsoFfile:despath];            } else {desimage = nil;    }} return desimage; }}//compressed picture + (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 picture size according to the screen size uiscreen *mainscreen = [UIScreen mainscreen];    Default is Portrait or portraitupsidedown cgsize baseimgsize = Cgsizemake (Base_img_width, base_img_height);    uideviceorientation orientation = [Uidevice currentdevice].orientation; if (orientation = = Uideviceorientationlandscapeleft | | orientation = = uideviceorientationlandscaperight) {//Lan    Dscapeleft or Landscaperight baseimgsize = Cgsizemake (Base_img_height, base_img_width); } cgsize newSize = Cgsizemake (SrcImage.size.width * SRCIMAGE.SCale * (Cgrectgetwidth (mainscreen.bounds) * mainscreen.scale)/baseimgsize.width, SrcImage.size.height *        Srcimage.scale * (Cgrectgetheight (mainscreen.bounds) * Mainscreen.scale)/baseimgsize.height);        Reset image UIImage *newimage = [UIImage imagewithimage:srcimage scaletosize:newsize];        Compress the picture NSData *imagedata = UIImageJPEGRepresentation (NewImage, compressionquality); Save new picture return [ImageData Writetofile:despath atomically:yes];} Reset the full name of the picture (with extension) + (NSString *) Desimagename: (NSString *) srcimagename withext: (NSString *) extname{return [Srcimagename s Tringbyappendingstring:extname];} Get the name of the picture (without extension) + (NSString *) Getimagename: (NSString *) srcimage{if (srcimage) {Nsarray *temparray = [Srcima                GE componentsseparatedbystring:@ "."];  if (Temparray) {//have. split file name if ([Temparray count] > 1) {nsstring                                *extname = [Temparray lastobject]; Strict judgmentFile extension if ([Extname isequaltostring:@ "png"] | | [Extname isequaltostring:@ "jpg"] | |                [Extname isequaltostring:@ "JPEG"])                {return [Srcimage substringwithrange:nsmakerange (0, Srcimage.length-(extname.length + 1)]);                } else {return nil;            }} else {return srcimage;        }} 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


3x Image Solution for IOS only (with source)

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.