先看一下打印出來的效果:
然后說我遇到的問題:
第一個問題:
打印機型號不好說鳍烁,所有的包裝盒蹲缠、說明書和機器上都是英文MPT-II斋射。同事說是從賣家手里買的,沒有客服蕾额,不能找他們解決問題早芭。里面有個光盤,里面有一些exe文件和iOS demo诅蝶,可是退个,這個demo運行報錯募壕!
報錯的是開發(fā)商提供的print.framework,這報錯我直接放棄了他的demo语盈。從GitHub上找了一些藍牙打印小票的demo舱馅,試了一下,都能連接這個打印機刀荒。但是呢代嗤,打印出來的中文都是亂碼。最后缠借,有兩個編碼需要設(shè)置
第一個看下面代碼干毅,第二個就是UTF-8
- (void)setText:(NSString *)text
{
NSStringEncoding enc = CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingGB_18030_2000);
NSData *data = [text dataUsingEncoding:enc];
[_printerData appendData:data];
}
第二個問題:
打印出來的亂碼,我就覺得是編碼問題烈炭,但是把編碼格式都試了一下溶锭,浪費了很多紙,打印出來還是亂碼符隙。
我就用光盤里面的軟件刷入了一下字體趴捅。
刷入之后,可以了霹疫,但是打印出來的有問題拱绑。
打印出來的有兩個問題,1丽蝎、字體都一樣大猎拨,也沒有粗體設(shè)置;2屠阻、圖片打印出來的效果都是下面有一部分缺失了红省。。国觉。
第三個問題:打印出來大字吧恃,圖片不缺失
先解決打印出來的粗體和大字,刷入一下
刷入之后呢麻诀,字體只是變粗了一些而已痕寓,設(shè)置字號0x77,7倍大就是打印不出來。
我就放棄了這種打印出來大字體的想法蝇闭。
那應(yīng)該怎么做才能打印出來大大的叫號呢呻率?
圖片,對呻引,把K017弄成圖片礼仗。
#pragma mark - view生成image 高清方法
- (UIImage *)makeImageWithView:(UIView *)view withSize:(CGSize)size
{
UIGraphicsBeginImageContextWithOptions(size, NO, 0.0);
[view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
做法很簡單,就是寫一個UIView
這個代碼我就不寫了,寫一個白背景色的UIView藐守,放進去一個UILabel挪丢,設(shè)置字號50,加粗卢厂,居中(記得把生成的圖片保存到相冊,以便參考對比)
就是這個效果了惠啄。
下面解決慎恒,圖片不缺失了。我就想著撵渡,我打印了兩個圖片下面都缺失了融柬,那我都把他們放進我的UIView里面,全部打印出來趋距,下面設(shè)置大的空白footer粒氧,就不會缺失了吧。
嘿嘿嘿节腐,這樣可以了吧外盯,看??:
這是什么鬼啊,出現(xiàn)亂碼翼雀,是因為沒有藍牙傳輸沒有分包嗎饱苟,我車看了代碼,有分包處理代碼狼渊。百度半天箱熬,可能這個包設(shè)置的有點大,我看了一下是146狈邑,提示必須設(shè)置偶數(shù)哦城须,我就改的小一點128試試,這個數(shù)字比較有感覺米苹。
跟我猜想的一樣糕伐,打印出來很完整的圖片。但是有一個重大的問題就是驱入,打印很慢赤炒,一卡一卡的,突突突的才打印出來亏较,而且打印出來的下面說明文字很小莺褒,基本看不清,我就放棄了打印一整張圖片的想法雪情。
第四個問題:尋找最優(yōu)解
把圖片分開打印遵岩,多次嘗試之后,決定,把銀行l(wèi)ogo和叫號生成新的圖片來打映局础:
下面文字的用text方式打印舍哄,第二張圖片直接打印,因為我沒找到demo里面可以任意布局的方法誊锭,把二維碼和電話分開打印表悬,最終效果就是我的第一張圖片。
下面上關(guān)鍵代碼:
//這段代碼放進AppDelegate.m丧靡,如果之前連接打印成功了蟆沫,再次打開App之后,就可以直接打印温治,不用再手動連接饭庞。
//自動重連打印機
NSString *idfv = [[[UIDevice currentDevice] identifierForVendor] UUIDString];
//為什么用IDFV呢,自己體會吧熬荆,作用很大舟山。
if (![[SEPrinterManager sharedInstance] isConnected]&&[[NSUserDefaults standardUserDefaults]boolForKey:idfv]) {
[[SEPrinterManager sharedInstance] autoConnectLastPeripheralTimeout:10 completion:^(CBPeripheral *perpheral, NSError *error) {
//這里不需要處理是否出錯
}];
}
-(void)StartPrint{
NSString *idfv = [[[UIDevice currentDevice] identifierForVendor] UUIDString];
if ([[NSUserDefaults standardUserDefaults]boolForKey:idfv]) {
[self OnlinePrint];
}else{
HBPrintViewController *vc=[[HBPrintViewController alloc]init];
vc.printer=[self getPrinter];
vc.modalPresentationStyle = UIModalPresentationOverCurrentContext;
[self.viewController presentViewController:vc animated:YES completion:^{
vc.view.backgroundColor=[UIColor colorWithWhite:0.9 alpha:0.1];
}];
}
}
//已經(jīng)連接過的藍牙設(shè)備開始打印,如果completion為NO卤恳,重新初始化發(fā)起藍牙連接
-(void)OnlinePrint{
HLPrinter *printer = [self getPrinter];
NSData *mainData = [printer getFinalData];
[[SEPrinterManager sharedInstance] sendPrintData:mainData completion:^(CBPeripheral *connectPerpheral, BOOL completion, NSString *error) {
NSLog(@"寫入結(jié)果:%d:%@",completion,error);
if (!completion) {
//重新發(fā)起初始化 設(shè)置NO
NSString *idfv = [[[UIDevice currentDevice] identifierForVendor] UUIDString];
[[NSUserDefaults standardUserDefaults] setBool:NO forKey:idfv];
[self StartPrint];
}
}];
}
- (HLPrinter *)getPrinter
{
HLPrinter *printer = [[HLPrinter alloc] init];
UIView *myView=[[UIView alloc]init];
myView.backgroundColor=[UIColor whiteColor];
myView.frame=CGRectMake(0, 0, 420, 250);
UIImage *img1=[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"Print1" ofType:@"pic"]];
UIImageView *imgV1 = [[UIImageView alloc]initWithImage:img1];
imgV1.frame=CGRectMake(10, 10, 400, 100);
[myView addSubview:imgV1];
UILabel *l=[[UILabel alloc]initWithFrame:CGRectMake(150, 150, 120, 60)];
l.text=self.model.queueNum;
l.font=[UIFont boldSystemFontOfSize:50];
l.textAlignment=NSTextAlignmentCenter;
l.textColor=[UIColor blackColor];
[myView addSubview:l];
UIImage *MYimg=[self makeImageWithView:myView withSize:CGSizeMake(420, 250)];
UIImageWriteToSavedPhotosAlbum(MYimg, nil, nil, nil);
[printer appendImage:MYimg alignment:HLTextAlignmentCenter maxWidth:368];
[printer appendText:[NSString stringWithFormat:@"等待人數(shù):%@",self.model.waitNum] alignment:HLTextAlignmentLeft];
[printer appendText:[NSString stringWithFormat:@"網(wǎng)點名稱:%@",self.model.orgName] alignment:HLTextAlignmentLeft];
[printer appendText:[NSString stringWithFormat:@"取號時間:%@",self.model.printTime] alignment:HLTextAlignmentLeft];
[printer appendText:@"請您在休息區(qū)等候累盗,注意呼號及電視屏幕顯示信息,如果過號纬黎,請您重新排號幅骄。" alignment:HLTextAlignmentLeft];
UIImage *img2=[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"Print0" ofType:@"pic"]];
[printer appendImage:img2 alignment:HLTextAlignmentCenter maxWidth:368];
return printer;
}
#pragma mark - view生成image 高清方法
- (UIImage *)makeImageWithView:(UIView *)view withSize:(CGSize)size
{
UIGraphicsBeginImageContextWithOptions(size, NO, 0.0);
[view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
//HBPrintViewController.m
#import "HBPrintViewController.h"
@interface HBPrintViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (strong, nonatomic) UITableView *tableView;
@property (strong, nonatomic) NSArray *deviceArray; /**< 藍牙設(shè)備個數(shù) */
@property (strong, nonatomic) UIViewController *presentingVC;
@end
@implementation HBPrintViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self.view addSubview:self.tableView];
self.tableView.delegate=self;
self.tableView.dataSource=self;
[self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
make.center.equalTo(self);
make.edges.equalTo(self).insets(UIEdgeInsetsMake(165, 260, 130, 260));
}];
SEPrinterManager *_manager = [SEPrinterManager sharedInstance];
[_manager startScanPerpheralTimeout:10 Success:^(NSArray<CBPeripheral *> *perpherals,BOOL isTimeout) {
NSLog(@"perpherals:%@",perpherals);
_deviceArray = perpherals;
[_tableView reloadData];
} failure:^(SEScanError error) {
NSLog(@"error:%ld",(long)error);
}];
}
-(UITableView *)tableView
{
if (!_tableView) {
_tableView=[[UITableView alloc]init];
_tableView.layer.cornerRadius=10;
UIView *v1=[[UIView alloc]initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 60)];
v1.backgroundColor=[UIColor whiteColor];
UILabel *l=[[UILabel alloc]initWithFrame:CGRectMake(0, 20, [UIScreen mainScreen].bounds.size.width, 40)];
l.text=@"請點擊您想連接的藍牙設(shè)備";
l.backgroundColor=[UIColor lightGrayColor];
[v1 addSubview:l];
_tableView.tableHeaderView=v1;
}
return _tableView;
}
//點別的地方返回之前頁面
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
if (self.presentingVC) {
[self dismissViewControllerAnimated:YES completion:nil];
}else
{
[self.presentingViewController dismissViewControllerAnimated:YES completion:nil];
}
}
#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return _deviceArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *identifier = @"deviceId";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:identifier];
}
CBPeripheral *peripherral = [self.deviceArray objectAtIndex:indexPath.row];
cell.textLabel.text = [NSString stringWithFormat:@"名稱:%@",peripherral.name];
cell.imageView.image = [UIImage imageNamed:@"blue"];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
CBPeripheral *peripheral = [self.deviceArray objectAtIndex:indexPath.row];
dispatch_queue_t queue = dispatch_queue_create("連接藍牙直接打印", NULL);
dispatch_async(queue, ^{
[[SEPrinterManager sharedInstance] fullOptionPeripheral:peripheral completion:^(SEOptionStage stage, CBPeripheral *perpheral, NSError *error) {
if (!error) {
NSLog(@"連接藍牙設(shè)備成功,但不一定是我的打印機哦");
}
if (stage == SEOptionStageSeekCharacteristics) {
NSData *mainData = [self.printer getFinalData];
[[SEPrinterManager sharedInstance] sendPrintData:mainData completion:^(CBPeripheral *connectPerpheral, BOOL completion, NSString *error)
{
if (completion) {
NSString *idfv = [[[UIDevice currentDevice] identifierForVendor] UUIDString];
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:idfv];
}
}];
}
}];
});
[self dismissViewControllerAnimated:YES completion:nil];
}
#pragma mark - 重寫dismiss解決方案
-(void)dismissViewControllerAnimated:(BOOL)flag completion:(void (^)(void))completion
{
if (self.presentedViewController)
{
if (self.presentingViewController) {
self.presentingVC = self.presentingViewController;
}
[super dismissViewControllerAnimated:flag completion:completion];
}
}
.h文件
#import <UIKit/UIKit.h>
#import "SEPrinterManager.h"
NS_ASSUME_NONNULL_BEGIN
@interface HBPrintViewController : UIViewController
@property(nonatomic , strong)HLPrinter *printer;
@end
NS_ASSUME_NONNULL_END
好了本今,到此我的關(guān)鍵代碼全在這里了拆座,至于引用的東西GitHub上就有。
完冠息,求贊求評論求喜歡挪凑,銀行信息這次不打碼了,就是這么任性逛艰!