iOS UIImage 常用 Category(类目)分享

西门桃桃 2020-08-01 PM 114℃ 0条

1、UIImage图像绘制、压缩等相关方法

+ (void)drawLinearGradient:(CGContextRef)context
                      path:(CGPathRef)path
                startColor:(CGColorRef)startColor
                  endColor:(CGColorRef)endColor
{
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    CGFloat locations[] = { 0.0, 1.0 };
    
    NSArray *colors = @[(__bridge id) startColor, (__bridge id) endColor];
    
    CGGradientRef gradient = CGGradientCreateWithColors(colorSpace, (__bridge CFArrayRef) colors, locations);
    
    
    CGRect pathRect = CGPathGetBoundingBox(path);
    
    //具体方向可根据需求修改
    CGPoint startPoint = CGPointMake(CGRectGetMinX(pathRect), CGRectGetMidY(pathRect));
    CGPoint endPoint = CGPointMake(CGRectGetMaxX(pathRect), CGRectGetMidY(pathRect));
    
    CGContextSaveGState(context);
    CGContextAddPath(context, path);
    CGContextClip(context);
    CGContextDrawLinearGradient(context, gradient, startPoint, endPoint, 0);
    CGContextRestoreGState(context);
    
    CGGradientRelease(gradient);
    CGColorSpaceRelease(colorSpace);
}

+ (UIImage *)colorImage:(CGSize )size startColor:(UIColor *)sColor endColor:(UIColor *)eColor
{
    UIGraphicsBeginImageContext(size);
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    
    //创建CGMutablePathRef
    CGMutablePathRef path = CGPathCreateMutable();
    
    //绘制Path
    CGRect rect = CGRectMake(0, 0, size.width, size.height);
    CGPathMoveToPoint(path, NULL, CGRectGetMinX(rect), CGRectGetMinY(rect));
    CGPathAddLineToPoint(path, NULL, CGRectGetMaxX(rect), CGRectGetMinY(rect));
    CGPathAddLineToPoint(path, NULL, CGRectGetMaxX(rect), CGRectGetMaxY(rect));
    CGPathAddLineToPoint(path, NULL, CGRectGetMinX(rect), CGRectGetMaxY(rect));
    CGPathCloseSubpath(path);
    
    UIColor *startColor = sColor; //[BDColor modeStringToColor:@"#21caac" withAlpha:1];
    UIColor *endColor = eColor; //[BDColor modeStringToColor:@"#42d99e" withAlpha:1];
    
    [self drawLinearGradient:ctx path:path startColor:startColor.CGColor endColor:endColor.CGColor];
    CGPathRelease(path);
    //从Context中获取图像,并显示在界面上
    UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    return img;
}

- (UIImage *)cutCircleImage {
    

    UIGraphicsBeginImageContextWithOptions(self.size, NO, 0.0);
    // 获取上下文
    CGContextRef ctr = UIGraphicsGetCurrentContext();
    //填充颜色
    CGContextSetFillColorWithColor(ctr, [UIColor whiteColor].CGColor);
    CGContextSaveGState(ctr);
    // 设置圆形
    CGRect rect = CGRectMake(0, 0, self.size.width, self.size.height);
    CGContextAddEllipseInRect(ctr, rect);
    // 裁剪
    CGContextClip(ctr);
    // 将图片画上去
    [self drawInRect:rect];
    
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return image;
}

+ (UIImage *)imageFromColor:(UIColor*)color
{
    CGSize size = CGSizeMake(1.0f, 1.0f);
    return [self imageFromColor:color imageSize:size];
}

+ (UIImage *)imageFromColor:(UIColor *)color imageSize:(CGSize)size
{
    CGRect rect = CGRectMake(0, 0,size.width, size.height);
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context, [color CGColor]);
    CGContextFillRect(context, rect);
    UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return img;
}

-(UIImage *) compressOriginalImage:(UIImage *)image toSize:(CGSize)size
{
    UIImage * resultImage = image;
    UIGraphicsBeginImageContext(size);
    [resultImage drawInRect:CGRectMake(00, 0, size.width, size.height)];
    UIImage * new_image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return new_image;
}

2、UIImage图像旋转方法

- (UIImage *)fixOrientation {
    
    // No-op if the orientation is already correct
    if (self.imageOrientation == UIImageOrientationUp) return self;
    
    // We need to calculate the proper transformation to make the image upright.
    // We do it in 2 steps: Rotate if Left/Right/Down, and then flip if Mirrored.
    CGAffineTransform transform = CGAffineTransformIdentity;
    
    switch (self.imageOrientation) {
        case UIImageOrientationDown:
        case UIImageOrientationDownMirrored:
            transform = CGAffineTransformTranslate(transform, self.size.width, self.size.height);
            transform = CGAffineTransformRotate(transform, M_PI);
            break;
            
        case UIImageOrientationLeft:
        case UIImageOrientationLeftMirrored:
            transform = CGAffineTransformTranslate(transform, self.size.width, 0);
            transform = CGAffineTransformRotate(transform, M_PI_2);
            break;
            
        case UIImageOrientationRight:
        case UIImageOrientationRightMirrored:
            transform = CGAffineTransformTranslate(transform, 0, self.size.height);
            transform = CGAffineTransformRotate(transform, -M_PI_2);
            break;
        case UIImageOrientationUp:
        case UIImageOrientationUpMirrored:
            break;
    }
    
    switch (self.imageOrientation) {
        case UIImageOrientationUpMirrored:
        case UIImageOrientationDownMirrored:
            transform = CGAffineTransformTranslate(transform, self.size.width, 0);
            transform = CGAffineTransformScale(transform, -1, 1);
            break;
            
        case UIImageOrientationLeftMirrored:
        case UIImageOrientationRightMirrored:
            transform = CGAffineTransformTranslate(transform, self.size.height, 0);
            transform = CGAffineTransformScale(transform, -1, 1);
            break;
        case UIImageOrientationUp:
        case UIImageOrientationDown:
        case UIImageOrientationLeft:
        case UIImageOrientationRight:
            break;
    }
    
    // Now we draw the underlying CGImage into a new context, applying the transform
    // calculated above.
    CGContextRef ctx = CGBitmapContextCreate(NULL, self.size.width, self.size.height,
                                             CGImageGetBitsPerComponent(self.CGImage), 0,
                                             CGImageGetColorSpace(self.CGImage),
                                             CGImageGetBitmapInfo(self.CGImage));
    CGContextConcatCTM(ctx, transform);
    switch (self.imageOrientation) {
        case UIImageOrientationLeft:
        case UIImageOrientationLeftMirrored:
        case UIImageOrientationRight:
        case UIImageOrientationRightMirrored:
            // Grr...
            CGContextDrawImage(ctx, CGRectMake(0,0,self.size.height,self.size.width), self.CGImage);
            break;
            
        default:
            CGContextDrawImage(ctx, CGRectMake(0,0,self.size.width,self.size.height), self.CGImage);
            break;
    }
    
    // And now we just create a new UIImage from the drawing context
    CGImageRef cgimg = CGBitmapContextCreateImage(ctx);
    UIImage *img = [UIImage imageWithCGImage:cgimg];
    CGContextRelease(ctx);
    CGImageRelease(cgimg);
    return img;
}

- (UIImage *)rotateWithOrientation:(UIImageOrientation)orientation{
    
    CGAffineTransform transform = CGAffineTransformIdentity;
    CGSize size = self.size;
    switch (orientation) {
        case UIImageOrientationUp:
            return self;
            break;
        case UIImageOrientationUpMirrored://图片原本状态 取镜像
            transform = CGAffineTransformMakeTranslation(size.width, size.height);//平移
            transform = CGAffineTransformRotate(transform, M_PI);//顺时针旋转180度
            break;
        case UIImageOrientationDown://将图像旋转180°
            transform = CGAffineTransformMakeTranslation(size.width, 0);//平移
            transform = CGAffineTransformScale(transform, -1.0, 1.0);//缩放,如果为负数则是将图片翻转,此处为沿Y轴翻转
            break;
        case UIImageOrientationDownMirrored://将图片旋转180°后取镜像
            break;
        case UIImageOrientationLeft://将图片逆时针旋转90° 此处用transform也可以实现 只是执行到第二步 “transform = CGAffineTransformRotate(transform, -M_PI/2);”的时候坐标系已经被翻转不方便计算,所以采用和CGContext结合的方式进行变换
            size = CGSizeMake(size.height, size.width);
            transform = CGAffineTransformMakeTranslation(size.width,0);//平移
            transform = CGAffineTransformRotate(transform, M_PI/2);
            break;
        case UIImageOrientationLeftMirrored:
            size = CGSizeMake(size.height, size.width);
            transform = CGAffineTransformMakeTranslation(size.width,0);//平移
            transform = CGAffineTransformRotate(transform, M_PI/2);
            break;
        case UIImageOrientationRight:
            size = CGSizeMake(size.height, size.width);
            transform = CGAffineTransformMakeTranslation(0, size.height);//平移
            transform = CGAffineTransformRotate(transform, -M_PI/2);
            break;
        case UIImageOrientationRightMirrored:
            
            size = CGSizeMake(size.height, size.width);
            transform = CGAffineTransformMakeTranslation(0, size.height);//平移
            transform = CGAffineTransformRotate(transform, -M_PI/2);
            break;
        default:
            break;
    }
    UIGraphicsBeginImageContext(size);
    CGContextRef contextRef = UIGraphicsGetCurrentContext();
    switch (orientation) {
        case UIImageOrientationLeft://将上述已经经过一次变化后的图形进行变换
        case UIImageOrientationRight:
            CGContextTranslateCTM(contextRef,0.0,size.height);
            CGContextScaleCTM(contextRef, 1.0, -1.0);//沿x轴翻转
            break;
        default:
            break;
    }
    CGContextConcatCTM(contextRef, transform);//
    CGContextDrawImage(UIGraphicsGetCurrentContext(),CGRectMake(0, 0, self.size.width, self.size.height), self.CGImage);//绘制图片
    UIImage * image = UIGraphicsGetImageFromCurrentImageContext();//取出图片
    UIGraphicsEndImageContext();
    return image;
}

3、UIImage图像拉伸

+ (UIImage *)resizableImageWithName:(NSString *)imageName
{
    
    // 加载原有图片
    UIImage *norImage = CCLiveAssetsImage(imageName);
    // 获取原有图片的宽高的一半
    CGFloat w = norImage.size.width * 0.5;
    CGFloat top = norImage.size.height * 0.9;
    CGFloat bottom = norImage.size.height-top;
    // 生成可以拉伸指定位置的图片
    UIImage *newImage = [norImage resizableImageWithCapInsets:UIEdgeInsetsMake(top, w, bottom, w) resizingMode:UIImageResizingModeStretch];
    
    return newImage;
}

非特殊说明,本博所有文章均为博主原创。

评论啦~