原图绘制新图,字符串绘制图片

#import <QuartzCore/QuartzCore.h>

#import <CoreGraphics/CoreGraphics.h>

#import <UIKit/UIKitDefines.h>



#define SCREENWIDTH ([UIScreen mainScreen].bounds.size.width)

#define SCREENHEIGHT ([UIScreen mainScreen].bounds.size.height)



//根据原有图片    绘制一个全屏宽,图等高两侧自定义背景色(红色)的图片


- (UIImage *)shot:(UIImage *)oldImage  backgroundColor:(UIColor *)color{

    

    CGSize size = CGSizeMake(SCREENWIDTH, oldImage.size.height);

    

    UIView *backView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREENWIDTH, size.height)];

    backView.backgroundColor = color;

    UIImageView *imageView = [[UIImageView alloc] initWithImage:oldImage];

    

    CGFloat imageX = (SCREENWIDTH - oldImage.size.width) * 0.5;

    

    imageView.frame = CGRectMake(imageX, 0, oldImage.size.width, oldImage.size.height);

    

    [backView addSubview:imageView];

    

    UIGraphicsBeginImageContext(backView.bounds.size);

    [backView.layer renderInContext:UIGraphicsGetCurrentContext()];

    UIImage *image= UIGraphicsGetImageFromCurrentImageContext();

    

    UIGraphicsEndImageContext();

    return image;

}



//根据原有图片    绘制一个全屏宽,图等高两侧自定义背景色(白色)的图片



- (UIImage *)snapshotScreenInView:(UIImage *)oldImage backgroundColor:(UIColor *)color{

    

    CGSize size = CGSizeMake(SCREENWIDTH, oldImage.size.height);

    UIView *backView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREENWIDTH, size.height)];

    

    backView.backgroundColor = color;

    

    UIImageView *imageView = [[UIImageView alloc] initWithImage:oldImage];

    

    CGFloat imageX = (SCREENWIDTH - oldImage.size.width) * 0.5;

    

    imageView.frame = CGRectMake(imageX, 0, oldImage.size.width, oldImage.size.height);

    

    [backView addSubview:imageView];


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

    CGRect rect = backView.frame;

    [backView drawViewHierarchyInRect:rect afterScreenUpdates:YES];

    

    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();

    

    return image;

}



//根据一个字符串,绘制一张图片,自定义:位置(居左,中,右),字体间距,大小,颜色,图片背景色



- (UIImage *)getImageWithString:(NSString *)string font:(UIFont *)font textColor:(UIColor *)color size:(CGSize)size {

    

    

    UILabel *label = [[UILabel alloc] init];

    label.backgroundColor = [UIColor whiteColor];

    if (size.width == MAXFLOAT) {

        //计算宽度

        label.numberOfLines = 1;

        label.frame = CGRectMake(0, 0, 0, size.height);

    }else if (size.height == MAXFLOAT) {

        //计算高度

        label.numberOfLines = 0;

        label.frame = CGRectMake(0, 0, size.width, 0);

    }

    

    NSMutableAttributedString *attributedText = [[NSMutableAttributedString alloc] initWithString:string];

    

    NSMutableParagraphStyle *paragraphStyle = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];

    paragraphStyle.lineBreakMode = NSLineBreakByCharWrapping;

    paragraphStyle.alignment = NSTextAlignmentCenter;

    

    

    [attributedText addAttributes:@{NSFontAttributeName:font,

                                    NSForegroundColorAttributeName:color,

                                    NSParagraphStyleAttributeName:paragraphStyle,

                                    NSKernAttributeName:@3} range:NSMakeRange(0, string.length)];

    

    label.attributedText = attributedText;

    [label sizeToFit];

    

    CGSize realSize = label.frame.size;

    

    //绘图关键

    UIGraphicsBeginImageContext(realSize);

    [label.layer renderInContext:UIGraphicsGetCurrentContext()];

    UIImage *image= UIGraphicsGetImageFromCurrentImageContext();

    

    UIGraphicsEndImageContext();

    

    return image;

}




猜你喜欢

转载自blog.csdn.net/liushihua147/article/details/70311906
今日推荐