Image processing tool class for app development process

Source: Internet
Author: User
Tags home screen

Let's start by listing the methods available in the tool class:

/** * Based on the original view and the frosted glass style, get the blur view, and automatically as the original view Subview (if you do not need to be a child view, call Removefromsuperview yourself)*/+ (UIView *) Getblureffectviewwithoriginalview: (UIView *) Originalview style: (imagehelperblureffectstyle) style;/** * Get new images based on original image and frosted glass style*/+ (UIImage *) Getblureffectimagewithoriginalimage: (UIImage *) Originalimage style: (imagehelperblureffectstyle) style;/** * Based on the original image, equal to the zoom factor, to obtain a new image*/+ (UIImage *) Getimagewithoriginalimage: (UIImage *) Originalimage scale: (cgfloat) scale;/** * According to the original image, equal to zoom maximum size, to obtain a new image*/+ (UIImage *) Getimagewithoriginalimage: (UIImage *) originalimage scalemaxsize: (cgsize) scalemaxsize;/** * Based on the original image, equal to the maximum size of the zoom, to obtain the new size*/+ (Cgsize) Getimagesizewithoriginalimage: (UIImage *) originalimage scalemaxsize: (cgsize) scalemaxsize;/** * According to the original image, completely fill the size, get the new image*/+ (UIImage *) Getimagewithoriginalimage: (UIImage *) originalimage fillsize: (cgsize) fillsize;/** * According to the original image, clipping area, get new image*/+ (UIImage *) Getimagewithoriginalimage: (UIImage *) originalimage cutframe: (cgrect) cutframe;/** * According to the color, get the solid color new image of unit size*/+ (UIImage *) Getimagewithcolor: (Uicolor *) color;/** * Based on view, get snapshot*/+ (UIImage *) Getsnapshotwithview: (UIView *) view;/** * Full screen, but not status bar*/+ (UIImage *) getfullscreensnapshot;

Description

1. Get the frosted glass effect (Gaussian blur) image

After IOS8, the class Uiblureffect andUivisualeffectview are provided, whichmakes it easy to generate a Gaussian blur view, and then only the subview of the target view is needed to see the effect. The iOS7 needs to be implemented on its own, but Apple provides a uiimage+imageeffects classification on the WWDC 2013 that generates Gaussian blur images. After the taxonomy is added to the project categories directory, it is referenced in the imagehelper. Because uiblureffectstyle occurs after iOS8, a custom Enumeration type Imagehelperblureffectstyle is defined, So that it can be used normally in iOS7. The implementation code is as follows:

+ (UIView *) Getblureffectviewwithoriginalview: (UIView *) Originalview style: (imagehelperblureffectstyle) style{if(Deviceiosversionabove (8) {Uiblureffectstyle blurstyle; Switch(style) { Caseimagehelperblureffectstyleextralight: {blurstyle=Uiblureffectstyleextralight;  Break; }             Caseimagehelperblureffectstylelight: {blurstyle=Uiblureffectstylelight;  Break; }             CaseImagehelperblureffectstyledark: {blurstyle=Uiblureffectstyledark;  Break; }} uiblureffect*effect =[Uiblureffect Effectwithstyle:blurstyle]; Uivisualeffectview*effectview =[[Uivisualeffectview alloc] initwitheffect:effect]; Effectview.frame=Originalview.bounds;        [Originalview Addsubview:effectview]; returnEffectview; } Else{UIImage*originalimage =[self getsnapshotwithview:originalview]; UIImage*blurimage =[self getblureffectimagewithoriginalimage:originalimage style:style]; Uiimageview*effectview =[[Uiimageview alloc] initWithFrame:originalView.bounds];                [Effectview Setimage:blurimage];                [Originalview Addsubview:effectview]; returnEffectview; }}+ (UIImage *) Getblureffectimagewithoriginalimage: (UIImage *) Originalimage style: (imagehelperblureffectstyle) style{UIImage*NewImage; Switch(style) { Caseimagehelperblureffectstyleextralight: {newimage=[Originalimage Applyextralighteffect];  Break; }         Caseimagehelperblureffectstylelight: {newimage=[Originalimage Applylighteffect];  Break; }         CaseImagehelperblureffectstyledark: {newimage=[Originalimage Applydarkeffect];  Break; }    }        returnnewimage;}

2. Provides a series of methods to scale the image, as well as the cropping method. The basic idea is to specify the drawing dimensions (the size of the ascended image) in the current image context, then draw the corresponding image to the specified position and then generate the final image. For example, crop the image sample code:

+ (UIImage *) Getimagewithoriginalimage: (UIImage *) originalimage cutframe: (cgrect) cutframe{    =  Cutframe.size;        Uigraphicsbeginimagecontext (newSize);    [Originalimage Drawinrect:cgrectmake (-cutframe.origin.x,-cutframe.origin.y, CutFrame.size.width, CutFrame.size.height)];     *newimage = uigraphicsgetimagefromcurrentimagecontext ();    Uigraphicsendimagecontext ();         return newimage;}

3. Screenshot method

You need to use the method:

Uigraphicsbeginimagecontextwithoptions (cgsize size, BOOL opaque, cgfloat scale ). The first parameter is still the drawing dimension for the specified image context, the second parameter specifies whether it is opaque, and the third is the equal scale factor, or 0.0, which indicates the same factor as the device's home screen.

Calayer's renderincontext: (cgcontextref) CTx method, which renders the layer all in one context, is recommended for the current image context. Finally get the image.

+ (UIImage *) Getsnapshotwithview: (UIView *) view{    0.0);    [View.layer Renderincontext:uigraphicsgetcurrentcontext ()];     *newimage = uigraphicsgetimagefromcurrentimagecontext ();    Uigraphicsendimagecontext ();         return newimage;} + (UIImage *) getfullscreensnapshot{    return  [self getsnapshotwithview:[ UIApplication Sharedapplication].keywindow];}

In full screen, the status bar is not included, because the status bar is not available directly on the application window. However, it is possible to get a screenshot of the system through the private API, not to explore.

Test content

UIImage *icon =LoadImage (AppIcon); UIImage*testimg; Testimg= [Imagehelper Getimagewithoriginalimage:icon scale:2]; LOG (@"%@", testimg); Testimg= [Imagehelper Getimagewithoriginalimage:icon scalemaxsize:cgsizemake ( -, -)]; LOG (@"%@", testimg); Testimg= [Imagehelper Getimagewithoriginalimage:icon fillsize:cgsizemake ( -, -)]; LOG (@"%@", testimg); Testimg= [Imagehelper Getimagewithoriginalimage:icon cutframe:cgrectmake (Ten,Ten, -, -)]; LOG (@"%@", testimg); Testimg= [Imagehelper Getimagewithcolor:color (255, -, -)]; LOG (@"%@", testimg); Testimg=[Imagehelper GetSnapshotWithView:self.view]; LOG (@"%@", testimg); Testimg=[Imagehelper Getfullscreensnapshot]; LOG (@"%@", testimg); Testimg=[Imagehelper getblureffectimagewithoriginalimage:testimg Style:imagehelperblureffectstyledark]; LOG (@"%@", testimg); UIView*coverview = [Imagehelper getblureffectviewwithoriginalview:[uiapplication sharedapplication].keywindow Style:I Magehelperblureffectstyledark];
 .- the- -  +: to:11.995 Base[33087:2301853] <uiimage:0x7ffaf97e8d00, { -, -} .- the- -  +: to:11.997 Base[33087:2301853] <uiimage:0x7ffaf97e9610, { -, -} .- the- -  +: to:11.999 Base[33087:2301853] <uiimage:0x7ffaf950a330, { -, -} .- the- -  +: to:12.001 Base[33087:2301853] <uiimage:0x7ffaf9463630, { -, -} .- the- -  +: to:12.002 Base[33087:2301853] <uiimage:0x7ffaf950a330, {1,1} .- the- -  +: to:12.007 Base[33087:2301853] <uiimage:0x7ffaf96004b0, {375,667} .- the- -  +: to:12.013 Base[33087:2301853] <uiimage:0x7ffaf950a330, {375,667} .- the- -  +: to:12.040 Base[33087:2301853] <uiimage:0X7FFAF9506F30, {375,667}

1. You can step through the code to view the contents of the testimg Image:

2.[imagehelper getsnapshotwithview:self. View]; Self.view in this line of code generates a snapshot with a size of {375, 667}, but if the Add code to the Viewdidload method:

Self.edgesforextendedlayout = Uirectedgenone;

the re-output size is {375, 603} because the Edgesforextendedlayout property defaults to Uirectedgeall, this should be noted when dealing with UI layouts. The Uiviewcontoller parent class for subsequent records is also mentioned.

The base project has been updated: [Email protected]:alongway/base.git

Image processing tool class for app development process

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.