#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 = [[UIViewalloc] initWithFrame:CGRectMake(0,0, SCREENWIDTH, size.height)];
backView.backgroundColor = color;
UIImageView *imageView = [[UIImageViewalloc] 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.layerrenderInContext:UIGraphicsGetCurrentContext()];
UIImage *image=UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
//根据原有图片 绘制一个全屏宽,图等高两侧自定义背景色(白色)的图片
- (UIImage *)snapshotScreenInView:(UIImage *)oldImage backgroundColor:(UIColor *)color{
CGSize size =CGSizeMake(SCREENWIDTH, oldImage.size.height);
UIView *backView = [[UIViewalloc] initWithFrame:CGRectMake(0,0, SCREENWIDTH, size.height)];
backView.backgroundColor = color;
UIImageView *imageView = [[UIImageViewalloc] 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, [UIScreenmainScreen].scale);
CGRect rect = backView.frame;
[backView drawViewHierarchyInRect:rectafterScreenUpdates:YES];
UIImage *image =UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
//根据一个字符串,绘制一张图片,自定义:位置(居左,中,右),字体间距,大小,颜色,图片背景色
- (UIImage *)getImageWithString:(NSString *)string font:(UIFont *)font textColor:(UIColor *)color size:(CGSize)size {
UILabel *label = [[UILabelalloc] init];
label.backgroundColor = [UIColorwhiteColor];
if (size.width ==MAXFLOAT) {
//计算宽度
label.numberOfLines =1;
label.frame =CGRectMake(0,0, 0, size.height);
}elseif (size.height ==MAXFLOAT) {
//计算高度
label.numberOfLines =0;
label.frame =CGRectMake(0,0, size.width,0);
}
NSMutableAttributedString *attributedText = [[NSMutableAttributedStringalloc] initWithString:string];
NSMutableParagraphStyle *paragraphStyle = [[NSParagraphStyledefaultParagraphStyle] 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.layerrenderInContext:UIGraphicsGetCurrentContext()];
UIImage *image=UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}