Создание штрих-кода на Watch OS 2

Как сгенерировать штрих-код на Apple Watch с Watch OS2, я могу сделать это с помощью API, такого как ZXing на iOS, но мне интересно, есть ли способ сделать то же самое в 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];
 }

person Syed Ismail Ahamed    schedule 16.09.2015    source источник


Ответы (3)


Я понял это, сгенерировав изображение в приложении 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]];

}
person Syed Ismail Ahamed    schedule 16.09.2015

Создайте приложение для iOS, а затем перенесите его на часы. Удачи!

person Fahri Azimov    schedule 16.09.2015

Проверьте эту библиотеку: EFQRCode.

Как следует из его документа, исходная реализация взята из swift_qrcodejs, то есть Cross-appleOS SIMPLE QRCode generator for swift, without using CIFilter.

person Han He    schedule 10.12.2018