【iOS】生成二维码
程序员文章站
2022-03-10 13:14:48
直接上干货#import NS_ASSUME_NONNULL_BEGIN@interface QRCodeGenerator : NSObject+(instancetype)shareInstance;- (UIImage *)creatQRGeneratorWithCenterImage:(UIImage *)centerImage textStr:(NSString*)str;@endNS_ASSUME_NON....
- 直接上干货
#import <Foundation/Foundation.h>
NS_ASSUME_NONNULL_BEGIN
@interface QRCodeGenerator : NSObject
+(instancetype)shareInstance;
- (UIImage *)creatQRGeneratorWithCenterImage:(UIImage *)centerImage textStr:(NSString*)str;
@end
NS_ASSUME_NONNULL_END
#import "QRCodeGenerator.h"
static QRCodeGenerator * _instance;
@implementation QRCodeGenerator
+ (instancetype)shareInstance{
static dispatch_once_t onceToken;
dispatch_once(&onceToken,^{
_instance = [[self alloc] init];
});
return _instance;
}
- (UIImage *)creatQRGeneratorWithCenterImage:(UIImage *)centerImage textStr:(NSString*)str{
/// 1.创建二维码过滤器
CIFilter * qrFilter = [CIFilter filterWithName:@"CIQRCodeGenerator"];
/// 2.设置默认值
[qrFilter setDefaults];
[qrFilter setValue:[str dataUsingEncoding:NSUTF8StringEncoding] forKey:@"inputMessage"];
// 4.获取二维码的图片
CIImage *ciimage = qrFilter.outputImage;
// 放大图片的比例
// ciimage = [ciimage imageByApplyingTransform:CGAffineTransformMakeScale(9, 9)];
/// 返回image
UIImage *newImage = [self createNonInterpolatedUIImageFormCIImage:ciimage withSize:300];
// return [UIImage imageWithCIImage:ciimage] ;
return [self qrGeneratorAddCenterImage:centerImage originalImage:newImage];
}
//--生成高清二维码
- (UIImage *)createNonInterpolatedUIImageFormCIImage:(CIImage *)image withSize:(CGFloat) size {
CGRect extent = CGRectIntegral(image.extent);
CGFloat scale = MIN(size/CGRectGetWidth(extent), size/CGRectGetHeight(extent));
// 创建 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);
// 保存 bitmap 到图片
CGImageRef scaledImage = CGBitmapContextCreateImage(bitmapRef);
CGContextRelease(bitmapRef);
CGImageRelease(bitmapImage);
return [UIImage imageWithCGImage:scaledImage];
}
-(UIImage *)qrGeneratorAddCenterImage:(UIImage *)image originalImage:(UIImage *)originalImage{
/// 开启图形上下文
UIGraphicsBeginImageContext(originalImage.size);
/// 画二维码的图片
[originalImage drawInRect:CGRectMake(0, 0, originalImage.size.width, originalImage.size.height)];
CGFloat meImageW = 50;
CGFloat meImageH = 50;
CGFloat meImageX = (originalImage.size.width - meImageW) * 0.5;
CGFloat meImageY = (originalImage.size.height - meImageH) * 0.5;
/// 画中心图片
[image drawInRect:CGRectMake(meImageX, meImageY, meImageW, meImageH)];
/// 获取图片
UIImage *finalImage = UIGraphicsGetImageFromCurrentImageContext();
/// 关闭图形上下文
UIGraphicsEndImageContext();
return finalImage;
}
- 这是生成中间带图片的
请大家关注我的个人公众号
本文地址:https://blog.csdn.net/Emperor_huanzi/article/details/114319359
推荐阅读
-
iOS9下UICollectionViewCell的awakeFromNib问题
-
WinForm生成验证码图片的方法
-
iOS----------componentsJoinedByString 和 componentsSeparatedByString 的方法的区别
-
idea类注释模板快捷键(idea自动生成方法注释快捷键)
-
ASP.NET MVC生成静态页面的方法
-
微信iOS6.2聊天记录怎么快速导入新手机
-
你升级了吗?苹果宣布iOS 12更新率已超70%
-
基于ASP.NET Core数据保护生成验证token示例
-
solidworks2014中怎么生成工程图?
-
爱剪辑怎么加二维码? 快剪辑将二维码导入视频的教程