iOS HD 바이두 QR코드 생성

2192 단어
#import "ViewController.h"

@interface ViewController ()

@property (weak, nonatomic) IBOutlet UIImageView *qrcodeView;


@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    
    // Do any additional setup after loading the view, typically from a nib.
}


-(void)touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event

{
    
    // 1.    
    
    CIFilter*filter =    [   CIFilter  filterWithName:@"CIQRCodeGenerator"];
    
    // 2.    (      ,               )
    
    [filter setDefaults];
    
    // 3.            
    
    NSData*data = [@"IOS     "dataUsingEncoding:NSUTF8StringEncoding];
    
    // 4.    (KVC)
    
    [filter setValue:data forKey:@"inputMessage"];
    
    // 5.     ,           
    
    CIImage*image = [filter outputImage];
    
    self.qrcodeView.image= [self createNonInterpolatedUIImageFormCIImage:image withSize:400];
    
}

/**
 
 *  CIImage       UIImage
 
 *
 
 *@paramimage CIImage
 
 *@paramsize    
 
 */

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

}

좋은 웹페이지 즐겨찾기