iOS 二维码


-( void ) createERvvvWithContent:( NSString *)content{
    //content :表示扫描二维码后显示的内容,传入URL可以跳转到相应的网页
     CIImage *ciImage = [ self createQRForString :content];
     UIImage *image = [ self  createNonInterpolatedUIImageFormCIImage :ciImage withSize : 500 ];   
     UIImage *resultImage = [ self imageBlackToTransparent :image  withRed : 0.93333 andGreen : 0.93333 andBlue : 0.93333 ]; // 生成的二维码图片
}

// 生成二维码
- ( CIImage *)createQRForString:(NSString*)qrString {
     NSData *stringData = [qrString  dataUsingEncoding : NSUTF8StringEncoding ];
     // 创建 filter
     CIFilter *qrFilter = [ CIFilter filterWithName : @"CIQRCodeGenerator" ];
     // 设置内容和纠错级别
     [qrFilter  setValue:stringData forKey:@"inputMessage"];
     [qrFilter  setValue:@"M" forKey:@"inputCorrectionLevel"];
     // 返回 CIImage
     return qrFilter. outputImage ;
}

// CIImage 转换成 UIImage
- ( 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];
}

// 对二维码进行颜色填充
void ProviderReleaseData ( void *info, const void *data, size_t  size){
     free (( void *)data);
}
- ( UIImage *)imageBlackToTransparent:( UIImage *)image withRed:( CGFloat )red andGreen:( CGFloat )green andBlue:( CGFloat )blue{
     const int imageWidth = image. size . width ;
     const int imageHeight = image. size . height ;
     size_t       bytesPerRow = imageWidth * 4 ;
     uint32_t * rgbImageBuf = ( uint32_t *) malloc (bytesPerRow * imageHeight);
     CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB ();
     CGContextRef context = CGBitmapContextCreate (rgbImageBuf, imageWidth,imageHeight, 8 , bytesPerRow, colorSpace, kCGBitmapByteOrder32Little kCGImageAlphaNoneSkipLast );
            CGContextDrawImage (context, CGRectMake ( 0 , 0 , imageWidth, imageHeight), image. CGImage );
     // 遍历像素
     int  pixelNum = imageWidth * imageHeight;
     uint32_t  * pCurPtr = rgbImageBuf;
     for ( int i = 0 ; i < pixelNum; i++, pCurPtr++){
          if ((*pCurPtr & 0xFFFFFF00 ) < 0x99999900 )    // 将白色变成透明
              { // 改成下面的代码,会将图片转成想要的颜色
                uint8_t * ptr = ( uint8_t *)pCurPtr;
             ptr[ 3 ] = red; //0~255
             ptr[ 2 ] = green;
             ptr[ 1 ] = blue;
             }      
          else
             {      
               uint8_t * ptr = ( uint8_t *)pCurPtr;
             ptr[ 0 ] = 0 ;
            }
     }
     // 输出图片
     CGDataProviderRef dataProvider = CGDataProviderCreateWithData ( NULL , rgbImageBuf, bytesPerRow * imageHeight, ProviderReleaseData );
     CGImageRef imageRef = CGImageCreate (imageWidth, imageHeight, 8 , 32 , bytesPerRow, colorSpace, kCGImageAlphaLast | kCGBitmapByteOrder32Little , dataProvider,   NULL , true , kCGRenderingIntentDefault );
     CGDataProviderRelease (dataProvider);
     UIImage * resultUIImage = [ UIImage imageWithCGImage :imageRef];
     // 清理空间
     CGImageRelease (imageRef);
     CGContextRelease (context);
     CGColorSpaceRelease (colorSpace);
     return  resultUIImage;
}


// 图片压缩到指定大小
- ( UIImage *)imageByScalingAndCroppingForSize:( CGSize )targetSize SourceImge:( UIImage *)sourceImg
{
     UIImage  *sourceImage = sourceImg;
      UIImage *newImage = nil ;
     CGSize imageSize = sourceImage. size ;
     CGFloat width = imageSize. width ;
     CGFloat height = imageSize. height ;
     CGFloat targetWidth = targetSize. width ;
     CGFloat targetHeight = targetSize. height ;
     CGFloat scaleFactor = 0.0 ;
     CGFloat  scaledWidth = targetWidth;
     CGFloat  scaledHeight = targetHeight;
     CGPoint thumbnailPoint = CGPointMake ( 0.0 , 0.0 );
     if ( CGSizeEqualToSize (imageSize, targetSize) == NO )
    {
             CGFloat  widthFactor = targetWidth / width;
             CGFloat  heightFactor = targetHeight / height;
             if (widthFactor > heightFactor)
                  scaleFactor = widthFactor; // scale to fit height
             else
                   scaleFactor = heightFactor; // scale to fit width

             scaledWidth= width * scaleFactor;
           scaledHeight = height * scaleFactor; // center the image
            if (widthFactor > heightFactor)
           {
                thumbnailPoint. y = (targetHeight - scaledHeight) * 0.5 ;
          }
           else if (widthFactor < heightFactor)
          {     
              thumbnailPoint. x = (targetWidth - scaledWidth) * 0.5 ;
           }
     }
     UIGraphicsBeginImageContext (targetSize); // this will crop
     CGRect thumbnailRect = CGRectZero ;
    thumbnailRect. origin = thumbnailPoint;
    thumbnailRect. size . width = scaledWidth;
    thumbnailRect. size . height = scaledHeight;
    [sourceImage  drawInRect :thumbnailRect];
    newImage = UIGraphicsGetImageFromCurrentImageContext ();
     if (newImage == nil )
          NSLog ( @"could not scale image" );
     //pop the context to get back to the default
     UIGraphicsEndImageContext ();
     return  newImage;
}

猜你喜欢

转载自blog.csdn.net/ssq940410/article/details/80051945