仿支付宝生成二维码并实现动画效果

//将后台获得的字符串转换为二维码

-(void)getQrcodeImagewithrcode:(NSString *)qrcode{

CIFilter *filter = [CIFilter filterWithName:@”CIQRCodeGenerator”];

[filter setDefaults];

NSString *dataString = qrcode;

NSData *data = [dataString dataUsingEncoding:NSUTF8StringEncoding];

[filter setValue:data forKeyPath:@”inputMessage”];

// 4.获取输出的二维码

CIImage *outputImage = [filter outputImage];

UIImageView *img = [[UIImageView alloc]init];

img.userInteractionEnabled = YES;

_qrcoImgView =img;

 

[self.view addSubview:img];

UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAction:)];

[img addGestureRecognizer:tapGesture];

 

[img mas_makeConstraints:^(MASConstraintMaker *make) {

make.top.equalTo(_qrcodeLabel.mas_bottom).offset(12);

make.centerX.equalTo(self.view.mas_centerX);

if (ScreenWidth == 320) {

make.width.equalTo(@130);

make.height.equalTo(@130);

}else{

make.width.equalTo(@190);

make.height.equalTo(@190);

}

}];

img.image = [self createNonInterpolatedUIImageFormCIImage:outputImage withSize:200];

}

– (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];

}

//放大上移

_qrcoImgView.transform = CGAffineTransformMakeScale(1.2, 1.2);

_qrcoImgView.transform = CGAffineTransformTranslate(_qrcoImgView.transform, 0, -100);

//恢复原状

_qrcoImgView.transform = CGAffineTransformMakeScale(1, 1);

_qrcoImgView.transform = CGAffineTransformTranslate(_qrcoImgView.transform, 0, 0);

Add a Comment

电子邮件地址不会被公开。 必填项已用*标注