来张渐变色的圆角图吧

可以控制垂直或者水平方向渐变, 其实给角度也可以, 先放着吧.

UIImage+LinearGradientColor.h

+ (UIImage *)as_imageWithLinearGradientColors:(NSArray *)colors;
+ (UIImage *)as_imageWithLinearGradientColors:(NSArray *)colors vertical:(BOOL)vertical;
+ (UIImage *)as_imageWithLinearGradientColors:(NSArray *)colors size:(CGSize)size;
+ (UIImage *)as_imageWithLinearGradientColors:(NSArray *)colors postions:(NSArray *)positions size:(CGSize)size;
+ (UIImage *)as_imageWithLinearGradientColors:(NSArray *)colors size:(CGSize)size vertical:(BOOL)vertical;
+ (UIImage *)as_imageWithLinearGradientColors:(NSArray *)colors postions:(NSArray *)positions size:(CGSize)size vertical:(BOOL)vertical;
+ (UIImage *)as_imageWithLinearGradientColors:(NSArray *)colors postions:(NSArray *)positions size:(CGSize)size vertical:(BOOL)vertical cornerRadius:(CGFloat)cornerRadius borderColor:(UIColor *)borderColor borderWidth:(CGFloat)borderWidth corners:(UIRectCorner)corners;

UIImage+LinearGradientColor.m

+ (UIImage *)as_imageWithLinearGradientColors:(NSArray *)colors{
    return [UIImage as_imageWithLinearGradientColors:colors
                                                  size:CGSizeZero];
}

+ (UIImage *)as_imageWithLinearGradientColors:(NSArray *)colors vertical:(BOOL)vertical{
    return [UIImage as_imageWithLinearGradientColors:colors
                                              postions:nil
                                                  size:CGSizeZero
                                              vertical:vertical];
}

+ (UIImage *)as_imageWithLinearGradientColors:(NSArray *)colors size:(CGSize)size{
    return [UIImage as_imageWithLinearGradientColors:colors
                                              postions:nil
                                                  size:size];
}

+ (UIImage *)as_imageWithLinearGradientColors:(NSArray *)colors postions:(NSArray *)positions size:(CGSize)size{
    return [UIImage as_imageWithLinearGradientColors:colors
                                              postions:positions
                                                  size:size
                                              vertical:YES];
}

+ (UIImage *)as_imageWithLinearGradientColors:(NSArray *)colors size:(CGSize)size vertical:(BOOL)vertical{
    return [UIImage as_imageWithLinearGradientColors:colors
                                              postions:nil
                                                  size:size
                                              vertical:vertical];
}

+ (UIImage *)as_imageWithLinearGradientColors:(NSArray *)colors postions:(NSArray *)positions size:(CGSize)size vertical:(BOOL)vertical{
    return [UIImage as_imageWithLinearGradientColors:colors
                                              postions:positions
                                                  size:size
                                              vertical:vertical
                                          cornerRadius:0.0f
                                           borderColor:nil
                                           borderWidth:0.0f
                                               corners:UIRectCornerAllCorners];
}

+ (UIImage *)as_imageWithLinearGradientColors:(NSArray *)colors postions:(NSArray *)positions size:(CGSize)size vertical:(BOOL)vertical cornerRadius:(CGFloat)cornerRadius borderColor:(UIColor *)borderColor borderWidth:(CGFloat)borderWidth corners:(UIRectCorner)corners{
    if (colors.count == 0){
        return nil;
    }

    if (CGSizeEqualToSize(size, CGSizeZero)){
        size = CGSizeMake(20.0f, 20.0f);
    }

    if (colors.count == 1){
        //退化成单色图片
        UIColor *color = colors.firstObject;
        if (![color isKindOfClass:[UIColor class]]){
            return nil;
        }

        //之前的纯色图片生成方法
        // http://adolsai.com/index.php/archives/uiimage-purecolorimage.html
        return [UIImage as_imageWithPureColor:color size:size];
    }

    if (positions.count == 0
        || [positions isKindOfClass:[NSArray class]]){
        CGFloat length = vertical ? size.height : size.width;
        CGFloat step = length / (colors.count - 1);
        NSMutableArray *mutablePositions = [[NSMutableArray alloc] init];
        [mutablePositions addObject:@0.0f];
        for (NSInteger i = 1; i < colors.count; i++){
            [mutablePositions addObject:@(step * i / length)];
        }
        positions = [mutablePositions copy];
    }

    if (colors.count != positions.count){
        return nil;
    }

    if (corners != UIRectCornerAllCorners) {
        UIRectCorner tmp = 0;
        if (corners & UIRectCornerTopLeft) tmp |= UIRectCornerBottomLeft;
        if (corners & UIRectCornerTopRight) tmp |= UIRectCornerBottomRight;
        if (corners & UIRectCornerBottomLeft) tmp |= UIRectCornerTopLeft;
        if (corners & UIRectCornerBottomRight) tmp |= UIRectCornerTopRight;
        corners = tmp;
    }

    CGRect imageRect = CGRectMake(0, 0, size.width, size.height);

    CGFloat *components = (CGFloat *)malloc(sizeof(CGFloat) * colors.count * 4);

    for (NSInteger i = 0; i < colors.count; i++) {
        UIColor *eachColor = [colors objectAtIndex:i];
        CGColorRef eachCGColor = eachColor.CGColor;
        const CGFloat *colorComponents = CGColorGetComponents(eachCGColor);

        CGFloat r,g,b,a;

        // canProvideRGBComponents
        CGColorSpaceModel spaceModel = CGColorSpaceGetModel(CGColorGetColorSpace(eachCGColor));
        BOOL canProvideRGBComponents = YES;
        if (spaceModel == kCGColorSpaceModelMonochrome) {
            canProvideRGBComponents = NO;
        }

        // r g b
        if (canProvideRGBComponents) {
            r = colorComponents[0];
            g = colorComponents[1];
            b = colorComponents[2];
        } else {
            r = colorComponents[0];
            g = colorComponents[0];
            b = colorComponents[0];
        }
        // a
        a = CGColorGetAlpha(eachCGColor);

        NSInteger p = i * 4;
        components[p] = r;
        components[p + 1] = g;
        components[p + 2] = b;
        components[p + 3] = a;
    }

    CGFloat *locations = (CGFloat *)malloc(sizeof(CGFloat) * positions.count);

    for (NSInteger i = 0; i < positions.count; i++){
        NSNumber *locationNumber = [positions objectAtIndex:i];
        CGFloat thisLocation = locationNumber.floatValue;
        if (thisLocation > 1.0f
            || thisLocation < 0.0f){
            free(locations);
            free(components);
            return nil;
        }
        locations[i] = thisLocation;
    }

    UIGraphicsBeginImageContextWithOptions(size, NO, [UIScreen mainScreen].scale);

    CGColorSpaceRef deviceRGB = CGColorSpaceCreateDeviceRGB();
    CGContextRef context = UIGraphicsGetCurrentContext();
    size_t num_of_locations = colors.count;

    CGGradientRef gradient = CGGradientCreateWithColorComponents(deviceRGB, components, locations, num_of_locations);
    CGPoint startPoint = CGPointMake(0.0f, 0.0f);
    CGPoint endPoint = CGPointMake(size.width, 0.0f);
    if (vertical){
        endPoint = CGPointMake(0.0f, size.height);
    }

    [[UIColor clearColor] set];
    [[UIBezierPath bezierPathWithRoundedRect:imageRect
                           byRoundingCorners:corners
                                 cornerRadii:CGSizeMake(cornerRadius, borderWidth)] addClip];
    UIRectFill(imageRect);

    CGContextDrawLinearGradient(context, gradient, startPoint, endPoint, 0.0f);

    if (borderColor && size.width > 6.0f) {
        CGFloat scale = [UIScreen mainScreen].scale;
        CGFloat strokeInset = 0.0f;
        CGRect strokeRect = CGRectInset(imageRect, strokeInset, strokeInset);
        CGFloat strokeRadius = cornerRadius > scale / 2 ? cornerRadius - scale / 2 : 0;
        UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:strokeRect
                                                   byRoundingCorners:corners
                                                         cornerRadii:CGSizeMake(strokeRadius, borderWidth)];
        [path closePath];

        path.lineWidth = borderWidth;
        path.lineJoinStyle = kCGLineJoinMiter;
        [borderColor setStroke];
        [path stroke];
    }


    UIImage *result = UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();
    CGColorSpaceRelease(deviceRGB);
    CGGradientRelease(gradient);
    free(locations);
    free(components);

    return result;
}

标签:ios, uiimage, uikit, gradient color