#import "ShareContentView.h"
#import <CoreImage/CoreImage.h>
@implementation RecognitionInfoShareContentView
- (instancetype)initWithFrame:(CGRect)frame infoModel:(nonnull RecognitionInfoModel *)infoModel
{
self = [super initWithFrame:frame];
if (self) {
[self setupUIWithInfoModel:infoModel];
}
return self;
}
-(void)setupUIWithInfoModel:(RecognitionInfoModel *)infoModel {
//以6为基准
self.frame = CGRectMake(0, 0, 375.0, 667.0);
self.backgroundColor = UIColor.whiteColor;
UIImageView *imgvOfInfo = [[UIImageView alloc]init];
imgvOfInfo.contentMode = UIViewContentModeScaleAspectFill;
imgvOfInfo.clipsToBounds = YES;
[imgvOfInfo customSetImageWithImageURL:infoModel.image_url];
[self addSubview:imgvOfInfo];
imgvOfInfo.backgroundColor = [UIColor orangeColor];
[imgvOfInfo mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.left.right.equalTo(self).offset(0);
make.height.mas_equalTo(270);
}];
UILabel *txtOfTitle = [UILabel quickLabelWithFontSize:40 textColorHexStr:@"FFFFFF" parentView:self];
txtOfTitle.text = infoModel.keyword;
txtOfTitle.textAlignment = NSTextAlignmentCenter;
[self addSubview:txtOfTitle];
[txtOfTitle mas_makeConstraints:^(MASConstraintMaker *make) {
make.bottom.equalTo(imgvOfInfo.mas_bottom).offset(-10);
make.centerX.equalTo(self);
make.height.mas_equalTo(55);
}];
UIImageView *imgOfQRcode = [[UIImageView alloc]init];
// imgOfQRcode.backgroundColor = UIColor.cyanColor;
[self addSubview:imgOfQRcode];
[imgOfQRcode mas_makeConstraints:^(MASConstraintMaker *make) {
make.bottom.offset(-20);
make.left.offset(30);
make.width.height.mas_equalTo(76);
}];
imgOfQRcode.image = [self createQRCodeWithUrl:@"https://android.myapp.com/myapp/detail.htm?apkName=com.wantupai.app"];
UILabel *txtOfSlogan = [UILabel quickLabelWithFontSize:12 textColorHexStr:@"BBBCCD" parentView:self];
txtOfSlogan.text = @"万图拍,带你发现世界";
[self addSubview:txtOfSlogan];
[txtOfSlogan mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(imgOfQRcode.mas_right).offset(15);
make.centerY.equalTo(imgOfQRcode);
make.height.mas_equalTo(17);
}];
UILabel *txtOfDate_M_D = [UILabel quickLabelWithFontSize:30 textColorHexStr:@"333333" parentView:self];
txtOfDate_M_D.textAlignment = NSTextAlignmentCenter;
txtOfDate_M_D.text = @"5-24";
[txtOfDate_M_D mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(imgvOfInfo.mas_bottom).offset(15);
make.centerX.equalTo(self);
make.height.mas_equalTo(40);
}];
UILabel *txtOfDate_week = [UILabel quickLabelWithFontSize:15 textColorHexStr:@"BBBCCD" parentView:self];
txtOfDate_week.textAlignment = NSTextAlignmentCenter;
txtOfDate_week.text = @"农历四月二十 星期五";
[txtOfDate_week mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(txtOfDate_M_D.mas_bottom).offset(2);
make.centerX.equalTo(self);
make.height.mas_equalTo(20);
}];
UILabel *txtOfContent = [UILabel quickLabelWithFont:[UIFont boldSystemFontOfSize:14] textColor:HexColor(@"333333") parentView:self];
txtOfContent.text = infoModel.describe;
txtOfContent.numberOfLines = 0;
[txtOfContent mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.offset(20);
make.right.offset(-20);
make.top.equalTo(txtOfDate_week.mas_bottom).offset(20);
make.bottom.equalTo(imgOfQRcode.mas_top).offset(-20);
}];
// 时间 日期 的计算
NSDate *date = [NSDate date];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
formatter.dateFormat = @"MM-dd";
NSLog(@"Year: %@", [formatter stringFromDate:date]);
txtOfDate_M_D.text = [formatter stringFromDate:date];
NSCalendar *calendar = [NSCalendar calendarWithIdentifier:NSCalendarIdentifierChinese];
unsigned unitFlags = NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitWeekday;
// 获取不同时间字段的信息
NSDateComponents* comp = [calendar components: unitFlags
fromDate:date];
NSLog(@"农历是%ld月 %ld %ld " , comp.month, comp.day, comp.weekday);
NSArray *chineseMonths=[NSArray arrayWithObjects:@"正月", @"二月", @"三月", @"四月", @"五月", @"六月",@"七月", @"八月",@"九月", @"十月", @"冬月", @"腊月", nil];
NSArray *chineseDays=[NSArray arrayWithObjects: @"初一", @"初二", @"初三", @"初四", @"初五", @"初六", @"初七", @"初八", @"初九", @"初十", @"十一", @"十二", @"十三", @"十四", @"十五", @"十六", @"十七", @"十八", @"十九", @"二十",@"廿一", @"廿二", @"廿三", @"廿四", @"廿五", @"廿六", @"廿七", @"廿八", @"廿九", @"三十", nil];
NSArray *chineseWeeks = [NSArray arrayWithObjects:@"星期一", @"星期二", @"星期三", @"星期四", @"星期五", @"星期六", @"星期日", nil];
NSString *chineseM_str = [chineseMonths objectAtIndex:comp.month -1];
NSString *chineseD_str = [chineseDays objectAtIndex:comp.day -1];
NSString *chineseW_str = [chineseWeeks objectAtIndex:comp.weekday -1];
txtOfDate_week.text = [NSString stringWithFormat:@"农历%@%@ %@", chineseM_str, chineseD_str, chineseW_str];
[self setNeedsLayout];
[self layoutIfNeeded];
}
//view转为image
- (UIImage*)convertViewToImage {
CGSize s = self.bounds.size;
// 下面方法,第一个参数表示区域大小。第二个参数表示是否是非透明的。如果需要显示半透明效果,需要传NO,否则传YES。第三个参数就是屏幕密度了
UIGraphicsBeginImageContextWithOptions(s, YES, [UIScreen mainScreen].scale);
[self.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
- (UIImage *)createQRCodeWithUrl:(NSString *)url {
// 1. 创建一个二维码滤镜实例(CIFilter)
CIFilter *filter = [CIFilter filterWithName:@"CIQRCodeGenerator"];
// 滤镜恢复默认设置
[filter setDefaults];
// 2. 给滤镜添加数据
NSString *string = url;
NSData *data = [string dataUsingEncoding:NSUTF8StringEncoding];
// 使用KVC的方式给filter赋值
[filter setValue:data forKeyPath:@"inputMessage"];
// 3. 生成二维码
CIImage *image = [filter outputImage];
// 转成高清格式
UIImage *qrcode = [self createNonInterpolatedUIImageFormCIImage:image withSize:200];
// 添加logo
qrcode = [self drawImage:[UIImage imageNamed:@"shareIcon"] inImage:qrcode];
return qrcode;
}
// 将二维码转成高清的格式
- (UIImage *)createNonInterpolatedUIImageFormCIImage:(CIImage *)image withSize:(CGFloat) size {
CGRect extent = CGRectIntegral(image.extent);
CGFloat scale = MIN(size/CGRectGetWidth(extent), size/CGRectGetHeight(extent));
// 1.创建bitmap;
size_t width = CGRectGetWidth(extent) * scale;
size_t height = CGRectGetHeight(extent) * scale;
CGColorSpaceRef cs = CGColorSpaceCreateDeviceGray();
CGContextRef bitmapRef = CGBitmapContextCreate(nil, width, height, 8, 0, cs, (CGBitmapInfo)kCGImageAlphaNone);
CIContext *context = [CIContext contextWithOptions:nil];
CGImageRef bitmapImage = [context createCGImage:image fromRect:extent];
CGContextSetInterpolationQuality(bitmapRef, kCGInterpolationNone);
CGContextScaleCTM(bitmapRef, scale, scale);
CGContextDrawImage(bitmapRef, extent, bitmapImage);
// 2.保存bitmap到图片
CGImageRef scaledImage = CGBitmapContextCreateImage(bitmapRef);
CGContextRelease(bitmapRef);
CGImageRelease(bitmapImage);
return [UIImage imageWithCGImage:scaledImage];
}
// 添加logo
- (UIImage *)drawImage:(UIImage *)newImage inImage:(UIImage *)sourceImage {
CGSize imageSize; //画的背景 大小
imageSize = [sourceImage size];
UIGraphicsBeginImageContextWithOptions(imageSize, NO, 0.0);
[sourceImage drawAtPoint:CGPointMake(0, 0)];
//获得 图形上下文
CGContextRef context=UIGraphicsGetCurrentContext();
//画 自己想要画的内容(添加的图片)
CGContextDrawPath(context, kCGPathStroke);
// 注意logo的尺寸不要太大,否则可能无法识别
CGRect rect = CGRectMake(imageSize.width / 2 - 25, imageSize.height / 2 - 25, 50, 50);
// CGContextAddEllipseInRect(context, rect);
CGContextClip(context);
[newImage drawInRect:rect];
//返回绘制的新图形
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
@end