Github source code: UIImage + YYWebImage, githubuiimage
UIImage + YYWebImage is a classification in the YYWebImage (https://github.com/ibireme/YYWebImage), this classification encapsulates some common change methods of image, it is worth learning ~ (My version is 1.0.5) Prerequisites: 1. CoreGraphics is widely used. The first common method is UIKIT_EXTERN void uigraphicsbeginimagecontextwitexceptions (CGSize, BOOL opaque, CGFloat scale) NS_AVAILABLE_IOS (4_0); Apple official documentation here https://developer.apple.com/reference/uikit/1623912-uigraphicsbeginimagecontextwitho
Parameters
-
size
-
The size (measured in points) of the new bitmap context. This represents the size of the image returned byUIGraphicsGetImageFromCurrentImageContext()Function. To get the size of the bitmap in pixels, you must multiply the width and height values by the value inscaleParameter.
-
opaque
-
A Boolean flag indicating whether the bitmap is opaque. If you know the bitmap is fully opaque, specifytrueTo ignore the alpha channel and optimize the bitmap's storage. SpecifyingfalseMeans that the bitmap must include an alpha channel to handle any partially transparent pixels.
-
scale
-
The scale factor to apply to the bitmap. If you specify a value0.0, The scale factor is set to the scale factor of the device's main screen.
Considering that UIColor is transparent, the opaque parameter passed in this method is generally set to NO. Considering the screen Scale, the input scale parameter is generally set to 0 or self. scale; the document has written "This function may be called from any thread of your app. "So there is no problem in any thread ~~ Specific Method: 1, + (UIImage *) yy_imageWithColor :( UIColor *) color size :( CGSize) size; generate a solid color image of the specified size; method implementation is relatively simple, it is to create an image context, get the context, set it to the specified color, fill, get the image, and close the image context. As mentioned in the beginning, opaque is NO, scale is 0. If it is used as the background image, you can directly set the size to (), and then use the default system stretch effect to enlarge the image (UIViewContentModeScaleToFill ), in this way, the efficiency is relatively high; YYImage also provides + (nullable UIImage *) yy_imageWithColor :( UIColor *) color; this method implements this function. 2,-(void) yy_drawInRect :( CGRect) rect withContentMode :( UIViewContentMode) contentMode clipsToBounds :( BOOL) clips; I think this method is an internal method. I personally feel that it is unnecessary to release it, I have never used it in some scenarios. 3,-(nullable UIImage *) yy_imageByResizeToSize :( CGSize) size contentMode :( UIViewContentMode) contentMode; generate an image of the specified size in the specified mode; size indicates the size of the generated image, which is transparent. This method calls-(void) yy_drawInRect :( CGRect) rect withContentMode :( UIViewContentMode) contentMode clipsTo. Bounds: (BOOL) clips. The static CGRect _ YYCGRectFitWithContentMode (CGRect rect, CGSize size, UIViewContentMode mode) method is worth learning. This method is used to obtain the complete rect of the image after the zoom-in or zoom-out by mode. For example, for an image of 100*200, use the UIViewContentModeScaleAspectFit mode and put it in an imageView of 400*400, the resulting rect is (200,400, 0, 100). A 200*400 image is saved in a 400 * imageView in the UIViewContentModeScaleAspectFill mode, the resulting rect is (0,200,400,800 );...... Different modes will produce different rect results, and the generated images will be different. The calculation of the specific rect is determined based on the aspect ratio of the image and the aspect ratio of the target rect, rather than the specific width or height, you need to pay special attention to this. This is entirely a mathematical computation to achieve the effect of the scaling mode. I really admire the author here !!! -(UIImage *) yy_imageByResizeToSize: (CGSize) size. This method is not called, but is written again. The effect is the same as that of passing UIViewContentModeScaleToFill through contentModel ~ 4,-(nullable UIImage *) yy_imageByCropToRect :( CGRect) rect; Crop an area of the image. Note that the crop range is only related to the image size, not to the imageView size; if the rect is larger than the image size, it will have no effect. If the rect is smaller than the image size, the specified area will be cropped; 5,-(nullable UIImage *) yy_imageByInsetEdge :( UIEdgeInsets) insets withColor :( nullable UIColor *) color; crop images based on insets. This method is also awesome ~, Note that insets can take a negative number. If a negative number is used, it is equivalent to adding a border with the specified color to the original image. If the number is positive, the image is cropped Based on The insets, at this time, the color parameter is invalid 6,-(UIImage *) yy_imageByRoundCornerRadius :( CGFloat) radius
Corners :( UIRectCorner) corners
BorderWidth :( CGFloat) borderWidth
BorderColor :( UIColor *) borderColor borderLineJoin :( CGLineJoin) borderLineJoin; CGContextDrawImage (context, rect, self. CGImage); while the CGContext coordinate system is different from the UIKit coordinate system, so special processing is required; specific differences can refer to the http://blog.csdn.net/trandy/article/details/6617272 here he used to modify the coordinate system method to solve, so corners is also inverted, need special processing; note that this method will only set rounded corner, if the original image is not square, how can we set the page to not get a positive circle image? We need to cut the image into a square in advance. 7,-(nullable UIImage *) yy_imageByRotate :( CGFloat) radian S fitSize: (BOOL) fitSize; generate the image fitSize after the specified Rotation Angle and pass yes to the image. The image will be automatically compressed to make the image completely displayed. If no is passed, the image will be truncated; here, CGRectApplyAffineTransform returns "CGRect containing the smallest rectangle of the rotated rect", and the origin value may be negative. This method is drawn using the CGBitmapContextCreate method, I don't know much about UIGraphicsGetCurrentContext... I can understand the following sentence: CGContextTranslateCTM (context, + (newRect. size. width * 0.5), + (newRect. size. height * 0.5); CGContextRotateCTM (context, radians );
CGContextDrawImage (context, CGRectMake (-(width * 0.5),-(height * 0.5), width, height), self. CGImage );
CGImageRef imgRef = CGBitmapContextCreateImage (context );
UIImage * img = [UIImageimageWithCGImage: imgRef scale: self. scaleorientation: self. imageOrientation]; CGImageRelease (imgRef); here it is clever, it seems that the CGContext anchor is in (), and the image anchor is in the center, so first move the context to the center, then, draw from (-(width * 0.5),-(height * 0.5), then obtain CGImageRef, and use [uiimagewithcgimage: imgRef scale: self. scaleorientation: self. imageOrientation] It is worth learning to correct the coordinate system ~ (CGFloat) blurRadius tintColor :( nullable UIColor *) tintColor tintMode :( CGBlendMode) gradient saturation :( CGFloat) saturation maskImage :( nullable UIImage *) maskImage; fuzzy images are easy to use, but they are beyond the scope of my understanding... 9, + (nullableUIImage *) yy_imageWithSmallGIFData :( NSData *) data scale :( CGFloat) scale; the document says it is used for small gif, such as emoticons ~ Implementation is not understood. It is to be studied...