点击二维码,将生成二维码保存到相册(由于图片违规所以对二维码进行擦出)

UI编写就不放入文章中了,只书写主要方法demo放在这里了

1、生成二维码

调用方法

UIImage *qrcodeImg = [self getQRCodeWithContentTxt:_textField.text codeWidth:600 picImg:[UIImage imageNamed:@"img"]];
_QRCodeImage.image = qrcodeImg;

生成二维码函数

- (UIImage*)getQRCodeWithContentTxt:(NSString*)urlString codeWidth:(CGFloat)codeWidth picImg:(UIImage*)picImg{
    //使用名为 CIQRCodeGenerator过滤器创建一个CIFilter对象
    CIFilter *filter = [CIFilter filterWithName:@"CIQRCodeGenerator"];

    [filter setDefaults];

    NSData *data = [urlString dataUsingEncoding:NSUTF8StringEncoding];

    //通过kvo方式一个字符串生成二维码
    [filter setValue:data forKey:@"inputMessage"];

    //含文本信息二维码已经生成

    CIImage *filterImg = [filter outputImage];

    CIImage *imageOri = [CIImage imageWithCGImage:[self filterQRCodeWithCIImage:filterImg codeWidth:codeWidth].CGImage];

    UIImage *img = [UIImage imageWithCIImage:imageOri];//默认二维码

    //------------------------------------------------------

    //-------对二维码增添颜色,不需要可以跳过----------

    CIFilter* color_filter = [CIFilter filterWithName:@"CIFalseColor"];

    [color_filter setDefaults];

    [color_filter setValue:imageOri forKey:@"inputImage"];

    //设置二维码的颜色二维码都是由2个颜色组成,可自行改变尝试效果)

    [color_filter setValue:[CIColor colorWithRed:66/255.0 green:147/255.0 blue:82/255.0] forKey:@"inputColor0"];

    [color_filter setValue:[CIColor clearColor]forKey:@"inputColor1"];

    CIImage *colorImage = [color_filter outputImage];

    img = [UIImage imageWithCIImage:colorImage];//修改颜色二维码

    //-------对二维码增添颜色,不需要可以跳过----------

    //------------------------------------------------------

    //开启图形上下文(会变模糊)
    //UIGraphicsBeginImageContext(img.size);
    //开启图形上下文(防止虚化模糊)
    UIGraphicsBeginImageContextWithOptions(img.size,NO,[[UIScreen mainScreen]scale]);
    //将二维码的图片画入
    [img drawInRect:CGRectMake(0, 0, img.size.width, img.size.height)];

    //------------------------------------------------------

    //-------二维码中间插入小插图,不需要的可以跳过----------

    UIImage *centerImg = picImg;

    CGFloat centerW = img.size.width*0.2;

    CGFloat centerH = centerW;

    CGFloat centerX = (img.size.width-centerW)*0.5;

    CGFloat centerY = (img.size.height-centerH)*0.5;

    [centerImg drawInRect:CGRectMake(centerX, centerY, centerW, centerH)];

    //-------二维码中间插入小插图,不需要的可以跳过----------

    //------------------------------------------------------

    //5.3获取绘制好的图片

    UIImage *finalImg = UIGraphicsGetImageFromCurrentImageContext();

    //5.4关闭图像上下文

    UIGraphicsEndImageContext();

    return finalImg;

}

- (UIImage *)filterQRCodeWithCIImage:(CIImage *)image codeWidth:(CGFloat)codeWidth{
    CGRect extent = CGRectIntegral(image.extent);
    
    CGFloat scale = MIN(codeWidth/CGRectGetWidth(extent), codeWidth/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];
}

2、添加提示框

写在UIViewController中

// 添加提示框
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Save QRCode?" message:@"The QRCode will be saved in Camera Roll album." preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *saveAction = [UIAlertAction actionWithTitle:@"Save" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
    // 保存二维码图像
    [self saveQRCodeImage];
}];
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:nil];
[alertController addAction:saveAction];
[alertController addAction:cancelAction];
[self presentViewController:alertController animated:YES completion:nil];

3、保存图片相册

要提前info.plist文件中写访问相册权限,否则程序崩溃报错

- (void)saveQRCodeImage {
    // 保存图像,需要提前在info.plist文件中写访问相册权限,否则会报错
//    UIImageWriteToSavedPhotosAlbum(_QRCodeView.QRCodeImage.image, nil, nil, nil);
    UIImageWriteToSavedPhotosAlbum(_QRCodeView.QRCodeImage.image, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
}

- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo {
    NSString *title;
    NSString *message;
    
    if (!error) {
        title = @"Success!";
        message = @"The QRCode image saved successfully.";
    }
    else {
        title = @"Failed!";
        message = @"The QRCode image saved unsuccessfully, please try again later.";
    }
    
    // 使用alert view显示二维码保存状态
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *action = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:nil];
    [alert addAction:action];
    
    [self presentViewController:alert animated:YES completion:nil];
}

@end

原文地址:https://blog.csdn.net/qq_43441647/article/details/128983959

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任

如若转载,请注明出处:http://www.7code.cn/show_44554.html

如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱suwngjj01@126.com进行投诉反馈,一经查实,立即删除

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注