开篇
最近项目需要搞了几个相对独立的小功能,今天有空总结一下他们的实现思路和方法,并总结一点项目中帮同事解决的问题,在此立个flag,今天晚上分两篇写一下其中的主要内容,写不完不睡觉了,哈哈。
主要内容
- 生成带logo的二维码
- 截屏保存至相册(区域截屏或当前屏幕截屏)
ZXingObjC生成带logo二维码
利用ZXingObjC生成二维码在之前的文章中我们做过详细的介绍,可以参考之前的一篇文章:二维码的生成和使用
文章中介绍了二维码的生成和扫描,这里我们在介绍一下带logo二维码的生成,同时设置容错率和去除二维码生成时白色边框的问题。
根据后台返回的地址信息生成二维码图片
主要生成的代码如下:dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ dispatch_async(dispatch_get_main_queue(), ^{if ([_userDic objectForKey:@"url"] != nil) {successShareView = [[SinginShareView alloc]initWithFrame:self.view.bounds];[successShareView.iconImageView sd_setImageWithURL:[NSURL URLWithString:[_userDic objectForKey:@"headimg"]] placeholderImage:[UIImage imageNamed:@"placeholderImage_square"]];successShareView.namedLb.text = [_userDic objectForKey:@"name"];ZXMultiFormatWriter *writer = [[ZXMultiFormatWriter alloc] init];ZXEncodeHints *hints = [ZXEncodeHints hints];hints.encoding = NSUTF8StringEncoding;hints.dataMatrixShape =ZXDataMatrixSymbolShapeHintForceSquare;hints.margin = [NSNumber numberWithInt:0.];hints.errorCorrectionLevel = [ZXQRCodeErrorCorrectionLevel errorCorrectionLevelH];NSString *qrString = [_userDic objectForKey:@"url"];ZXBitMatrix *result = [writer encode:qrString format:kBarcodeFormatQRCode width:500 height:500 hints:hints error:nil];if (result) {CGImageRef image = [[ZXImage imageWithMatrix:result] cgimage];UIImage *image1 = [UIImage imageWithCGImage:image];//二维码原图UIImage *subIamge = [UIImage imageNamed:@"新签到切图logo@2x-X"];__weak typeof(self) weakSelf = self;successShareView.erweimaImageView.image =[weakSelf addSubImage:image1 sub:subIamge];//二维码里加图标,生成带logo的方法// successShareView.erweimaImageView.image =image1;//生成不带logo的二维码的方法 } else {successShareView.erweimaImageView.image = nil;}}successShareView.delegate =self;[self.view addSubview:successShareView];});});
二维码图片与logo图片合并的方法
-(UIImage *)addSubImage:(UIImage *)img sub:(UIImage *) subImage
{//get image width and heightint w = img.size.width;int h = img.size.height;int subWidth = 100*ratio_width;int subHeight =100*ratio_width;CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();//create a graphic context with CGBitmapContextCreateCGContextRef context = CGBitmapContextCreate(NULL, w, h, 8, 4 * w, colorSpace, kCGImageAlphaPremultipliedFirst);CGContextDrawImage(context, CGRectMake(0, 0, w, h), img.CGImage);CGContextDrawImage(context, CGRectMake( (w-subWidth)/2, (h - subHeight)/2, subWidth, subHeight), [subImage CGImage]);CGImageRef imageMasked = CGBitmapContextCreateImage(context);CGContextRelease(context);CGColorSpaceRelease(colorSpace);return [UIImage imageWithCGImage:imageMasked];
}
生成的带logo的二维码如下:
注意点:这里因为要设置logo的关系,可能会存在二维码信息被遮挡导致有个别信息不能被识别的情况发生,要注意以下两点:
- 设置生成二维码的容错率
hints.errorCorrectionLevel = [ZXQRCodeErrorCorrectionLevel errorCorrectionLevelH];
- 2.注意设置logo的大小 ,这里的logo大小要根据自己的二维码大小去设置。
int subWidth = 100*ratio_width;
int subHeight =100*ratio_width;
区域截屏并保存至相册
我们需要截屏的原来界面如下:
可以看到我们生成的要待保存的图片,包含灰色背景,和右上角的叉号,我们要保存的最终效果如下:
可以看到我们只保存了我们想要的部分控件到我们的相册。
实现的代码如下:
//截屏分享 传入想截屏的view(也可以是controller webview只能截当前屏幕-_-`,用其他的方法)
-(void)saveImageView
{UIGraphicsBeginImageContextWithOptions(successShareView.SinginShareView.bounds.size, NO, 0);CGContextRef ctx = UIGraphicsGetCurrentContext();[successShareView.SinginShareView.layer renderInContext:ctx];// 这个是要分享图片的样式(自定义的)UIImage * newImage = UIGraphicsGetImageFromCurrentImageContext();//保存到本地相机UIImageWriteToSavedPhotosAlbum(newImage,self,@selector(image:didFinishSavingWithError:contextInfo:),nil);}
保存图片至相册的回调方法
//保存相片的回调方法
- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo
{if (error) {[SVProgressHUD showErrorWithStatus:@"保存失败"];} else {[SVProgressHUD showSuccessWithStatus:@"成功保存到相册"];}
}
通过以上方式,我们可以保存我们自己想保存的控件和内容,但是在webview的时候只能保存当前页,如果想截屏整个webview的话需要增加代码,这里暂时就不放了。
后记
通过以上方法我们就实现了带logo二维码的生成,以及保存到本地相册,当然也可以把截屏的图片通过分享的方式分享给自己的好友,这里只是其中的一个使用方法。