分享的內(nèi)容,大家看了后,不管有什么問題或者建議,都可以說出來,我都會一一做答,一起加油啦
我的后面兩章文章<<iOS從相冊讀取二維碼>>,<<iOS二維碼的直接掃描的讀取>>介意借鑒學(xué)習(xí)
1.在需要生成二維碼的界面. ?
////// 生成二維碼生成的數(shù)據(jù)-根據(jù)公司的要求形成相應(yīng)的字符串
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];
//////將字典轉(zhuǎn)為jsonString
[self DataTOjsonString:dataDictionary];
///////就是具體的生成jsonString
#pragma mark - 得到j(luò)sonString
-(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;
}
//// 調(diào)用生成二維碼的方法
_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));
// 創(chuàng)建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? ? ? 響應(yīng)方法對象
*? SEL completionSelector? ? 方法
*? void *contextInfo
*/
UIImageWriteToSavedPhotosAlbum(img, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
}
// 相冊存入成功回調(diào)
- (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];
//? ? }
}