2018-07-19

生成二维码


self.QRcodeimg.image = [self createQRImageWithString:@"http://www.pengchao.vip" size:self.QRcodeimg.frame.size];

self.QRcodeimg.image = [self addImageForQRImage:self.QRcodeimg.image];


//生成制定大小的黑白二维码

- (UIImage*)createQRImageWithString:(NSString*)string size:(CGSize)size

{

    NSData *stringData = [string dataUsingEncoding:NSUTF8StringEncoding];

    CIFilter *qrFilter = [CIFilter filterWithName:@"CIQRCodeGenerator"];

    [qrFiltersetValue:stringDataforKey:@"inputMessage"];

    [qrFiltersetValue:@"M" forKey:@"inputCorrectionLevel"];

    CIImage*qrImage = qrFilter.outputImage;

    //放大并绘制二维码(上面生成的二维码很小,需要放大)

    CGImageRef cgImage = [[CIContext contextWithOptions:nil] createCGImage:qrImage fromRect:qrImage.extent];

    UIGraphicsBeginImageContext(size);

    CGContextRef context = UIGraphicsGetCurrentContext();

    CGContextSetInterpolationQuality(context, kCGInterpolationNone);

    //翻转一下图片 不然生成的QRCode就是上下颠倒的

    CGContextScaleCTM(context,1.0, -1.0);

    CGContextDrawImage(context, CGContextGetClipBoundingBox(context), cgImage);

    UIImage *codeImage = UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndPDFContext();

    CGImageRelease(cgImage);

    returncodeImage;

}


//在二维码中心加一个小图

- (UIImage*)addImageForQRImage:(UIImage*)qrImage

{

    UIGraphicsBeginImageContext(qrImage.size);


    [qrImagedrawInRect:CGRectMake(0,0, qrImage.size.width, qrImage.size.height)];


    UIImage *image = [UIImage imageNamed:@"xiaoxi_icon_QQ"];


    CGFloatimageW =50;

    CGFloatimaegX = (qrImage.size.width- imageW) *0.5;

    CGFloatimageY = (qrImage.size.height- imageW) *0.5;


    [imagedrawInRect:CGRectMake(imaegX, imageY, imageW, imageW)];


    UIImage *result = UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();


    returnresult;

}

你可能感兴趣的:(2018-07-19)