如何使用Watch OS2在苹果手表上生成条形码,我可以使用像iOS上的ZXing这样的API,但我想知道在watchOS2中有没有办法做同样的事情
NSError *error = nil;
ZXMultiFormatWriter *writer = [ZXMultiFormatWriter writer];
ZXBitMatrix* result;
//generate code 128 barcode
result= [writer encode:barCodeNumber
format:kBarcodeFormatCode128
width:500
height:500
error:&error];
if (result) {
CGImageRef image = [[ZXImage imageWithMatrix:result] cgimage];
return [UIImage imageWithCGImage:image];
}
答案 0 :(得分:1)
我通过在iOS应用中生成图像并通过背景传输将其传递给观看操作系统,通过从图像创建NSData来解决这个问题,这样的事情
- (void)viewDidLoad {
[super viewDidLoad];
if([WCSession isSupported]){
self.watchSession = [WCSession defaultSession];
self.watchSession.delegate = self;
[self.watchSession activateSession];
}
}
-(void)sendDatatoAppleWatch
{
NSMutableArray*barCodesArray=[[NSMutableArray alloc]init];
UIImage* barCodeImage=[self generateBarCode];
NSData *pngData = UIImagePNGRepresentation(barCodeImage);
[barCodeArray addObject:pngData]
if(self.watchSession){
NSError *error = nil;
if(![self.watchSession
updateApplicationContext:
@{@"cardData" : userCardsArray }
error:&error]){
NSLog(@"Updating the context failed: %@", error.localizedDescription);
UIAlertView* errorAlert=[[UIAlertView alloc]initWithTitle:error.localizedDescription message:error.debugDescription delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil];
[errorAlert show];
}
}
//*** Apple Watch Code**//
- (void)awakeWithContext:(id)context {
[super awakeWithContext:context];
if([WCSession isSupported]){
self.watchSession = [WCSession defaultSession];
self.watchSession.delegate = self;
[self.watchSession activateSession];
}
}
- (void) session:(WCSession *)session didReceiveApplicationContext:(NSDictionary<NSString *,id> *)applicationContext {
NSData* imageData = [[[applicationContext objectForKey:@"cardData"] objectAtIndex:0] valueForKey:@"barCodeImage"];
[self.barcodeImageView setImage:[UIImage imageWithData:imageData]];
}
答案 1 :(得分:0)
在iOS应用上生成,然后将其传输到手表。祝你好运!
答案 2 :(得分:0)
检查此库:EFQRCode。
根据其文档,原始实现来自swift_qrcodejs,即Cross-appleOS SIMPLE QRCode generator for swift, without using CIFilter
。