UIImage的Category总结

  • 之前代码丢失,现在又开始重新总结一下知识点,先来简单了解一下UIImage的扩展方法,此文会持续更新。。。
  • 基本知识:
    UIGraphicsBeginImageContext
    创建一个基于位图的上下文(context),并将其设置为当前上下文(context),
    参数size为新创建的位图上下文的大小。它同时是由UIGraphicsGetImageFromCurrentImageContext函数返回的图形大小。
    该函数的功能同UIGraphicsBeginImageContextWithOptions的功能相同,相当与UIGraphicsBeginImageContextWithOptions的opaque参数为NO,scale因子为1.0。

    UIGraphicsBeginImageContextWithOptions(CGSizeMake(sizeWith, sizeHeight), NO, 1.0);
    

    等价于

    UIGraphicsBeginImageContext(CGSizeMake(sizeWith, sizeHeight));
    

    size——同UIGraphicsBeginImageContext
    opaque—透明开关,如果图形完全不用透明,设置为YES以优化位图的存储。
    scale—–缩放因子

对图片加水印(图片的合并)

/**
 *  对图片进行水印处理
 *
 *  @param backgroundImage 背景图片
 *  @param markImage       水印图片
 *  @param markLocation    水印位置
 *
 *  @return 加水印后的图片
 */
+ (instancetype)waterMarkWithImage:(UIImage *)backgroundImage markImage:(UIImage *)markImage location:(CGRect)markLocation{

    CGFloat sizeWith = backgroundImage.size.width;
    CGFloat sizeHeight = backgroundImage.size.height;

    UIGraphicsBeginImageContextWithOptions(CGSizeMake(sizeWith, sizeHeight), NO, 0.0);
    [backgroundImage drawInRect:CGRectMake(0, 0, sizeWith, sizeHeight)];

    [markImage drawInRect:markLocation];

    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    return newImage;
}

对图片进行等比缩放

/**
 *  对图片进行等比缩放
 *
 *  @param scaleImage 缩放的图片
 *  @param scaleSize  缩放比例
 *
 *  @return 缩放后的图片
 */
 + (instancetype)scaleImage:(UIImage *)scaleImage toScale:(float)scaleSize{

    CGFloat sizeWith = scaleImage.size.width * scaleSize;
    CGFloat sizeHeight = scaleImage.size.height * scaleSize;

    UIGraphicsBeginImageContextWithOptions(CGSizeMake(sizeWith, sizeHeight), NO, 0.0);

    [scaleImage drawInRect:CGRectMake(0, 0, sizeWith, sizeHeight)];

    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    return newImage;
}

对图片进行自定义大小缩放

/**
 *  对图片进行自定义大小缩放
 *
 *  @param image      缩放的图片
 *  @param customSize 缩放的大小
 *
 *  @return 缩放后的图片
 */
 + (instancetype)customSizeImage:(UIImage *)image customSize:(CGSize)customSize{

    UIGraphicsBeginImageContextWithOptions(CGSizeMake(customSize.width, customSize.height), NO, 0.0);

    [image drawInRect:CGRectMake(0, 0, customSize.width, customSize.height)];

    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    return newImage;
}

对图片进行自定义剪切

/**
 *  对图片进行自定义剪切
 *
 *  @param image 需要剪切的图片
 *  @param rect 需要剪切的图片位置大小
 *
 *  @return 剪切后的图片
 */
 + (instancetype)customCutImage:(UIImage *)image AtRect:(CGRect)rect{

    CGImageRef subImageRef = CGImageCreateWithImageInRect(image.CGImage, rect);
    UIImage* subImage = [UIImage imageWithCGImage: subImageRef];
    CGImageRelease(subImageRef);

    return subImage;
}

对图片进行自定义旋转

定义两个c方法,方便转换

CGFloat DegreesToRadians(CGFloat degrees) {return degrees * M_PI / 180;};
CGFloat RadiansToDegrees(CGFloat radians) {return radians * 180/M_PI;};

对图片自定义角度旋转(以中心点为旋转点,以°为旋转单位,如90°)

/**
 *  对图片自定义角度旋转(以中心点为旋转点,以°为旋转单位,如90°)
 *
 *  @param image   需要旋转的图片
 *  @param degrees 旋转的角度
 *
 *  @return 旋转后的图片
 */
+ (instancetype)customImageRotate:(UIImage *)image rotatedDegrees:(CGFloat)degrees{

    UIView *rotatedViewBox = [[UIView alloc] initWithFrame:CGRectMake(0,0,image.size.width, image.size.height)];
    CGAffineTransform t = CGAffineTransformMakeRotation(DegreesToRadians(degrees));
    rotatedViewBox.transform = t;
    CGSize rotatedSize = rotatedViewBox.frame.size;

    UIGraphicsBeginImageContext(rotatedSize);
    CGContextRef bitmap = UIGraphicsGetCurrentContext();

    CGContextTranslateCTM(bitmap, rotatedSize.width/2, rotatedSize.height/2);

    CGContextRotateCTM(bitmap, DegreesToRadians(degrees));

    CGContextScaleCTM(bitmap, 1.0, -1.0);
    CGContextDrawImage(bitmap, CGRectMake(-image.size.width / 2, -image.size.height / 2, image.size.width, image.size.height), image.CGImage);

    UIImage *resImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return resImage;
}

对图片自定义角度旋转(以中心点为旋转点,以M_PI为旋转单位,如M_PI/4.0)

/**
 *  对图片自定义角度旋转(以中心点为旋转点,以M_PI为旋转单位,如M_PI/4.0)
 *
 *  @param image   需要旋转的图片
 *  @param radians 旋转的角度
 *
 *  @return 旋转后的图片
 */
 + (instancetype)customImageRotate:(UIImage *)image rotatedRadians:(CGFloat)radians{
    return [UIImage customImageRotate:image rotatedDegrees:RadiansToDegrees(radians)];
}