分享的内容,大家看了后,不管有什么问题或者建议,都可以说出来,我都会一一做答,一起加油啦
我的后面两章文章<<iOS从相册读取二维码>>,<<iOS二维码的直接扫描的读取>>介意借鉴学习
1.在需要生成二维码的界面.
////// 生成二维码生成的数据-根据公司的要求形成相应的字符串
NSDictionary *dataDictionary= [NSDictionary dictionaryWithObjectsAndKeys:@"PVTLfbmPU6FA01aD1XU20hkX38ew8Vh7Wg3JOXYDRsY=",@"ciphertext",@0,@"openTimes",@"2016-08-26 18:11:33",@"validFrom",@"2018-08-26 18:11:38",@"validTo",@"蓝牙钥匙",@"name",@"D5:CC:F1:13:92:7C",@"mac",@3,@"id",nil];
//////将字典转为jsonString
[self DataTOjsonString:dataDictionary];
///////就是具体的生成jsonString
#pragma mark - 得到jsonString
-(NSString*)DataTOjsonString:(id)object {
NSError *error;
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:object
options:NSJSONWritingPrettyPrinted // Pass 0 if you don't care about the readability of the generated string
error:&error];
if (! jsonData) {
NSLog(@"Got an error: %@", error);
} else {
_jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
}
return _jsonString;
}
//// 调用生成二维码的方法
_shareView.img.scanResultImg.image = [self qrCodeImageWithString:_jsonString];
/////具体生成二维码的方法
#pragma mark - 生成二维码
-(UIImage*)qrCodeImageWithString:(NSString*)str{
if (str && [str isKindOfClass:[NSString class]] && str.length > 0) {
return [self createNonInterpolatedUIImageFromCIImage :[self createQRForString:str]
withSize:180];
} else {
NSLog(@"nil-----");
}
return nil;
}
// 传入字符串-形成二维码
- (CIImage *)createQRForString:(NSString *)qrString
{
// Need to convert the string to a UTF-8 encoded NSData object
NSData *stringData = [qrString dataUsingEncoding: NSUTF8StringEncoding];
NSLog(@"--stringData--%@",stringData);
// Create the filter
CIFilter *qrFilter = [CIFilter filterWithName:@"CIQRCodeGenerator"];
NSLog(@"----qrFilter---%@",qrFilter);
// Set the message content and error-correction level
[qrFilter setValue:stringData forKey:@"inputMessage"];
[qrFilter setValue:@"L" forKey:@"inputCorrectionLevel"];
NSLog(@"--qrFilter.outputImage%@----",qrFilter.outputImage);
// Send the image back
return qrFilter.outputImage;
}
- (UIImage *)createNonInterpolatedUIImageFromCIImage:(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);
UIImage *img = [UIImage imageWithCGImage:scaledImage];
if (img) {
[self tapSaveImageToIphone:img];
return img;
} else {
return nil;
}
}
// 保存图片到相册
- (void)tapSaveImageToIphone:(UIImage *)img{
/**
* 将图片保存到iPhone本地相册
* UIImage *image 图片对象
* id completionTarget 响应方法对象
* SEL completionSelector 方法
* void *contextInfo
*/
UIImageWriteToSavedPhotosAlbum(img, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
}
// 相册存入成功回调
- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void*)contextInfo{
// if (error == nil) {
//
// UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"已存入手机相册" delegate:self cancelButtonTitle:nil otherButtonTitles:@"确定", nil, nil];
// [alert show];
//
// }else{
//
// UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"保存失败" delegate:self cancelButtonTitle:nil otherButtonTitles:@"确定", nil ,nil];
// [alert show];
// }
}
网友评论
[qrFilter setValue:@"L" forKey:@"inputCorrectionLevel"];