2015-09-16 54 views
1

如何生成蘋果手錶手錶OS2條形碼,我可以使用API​​的喜歡斑馬線上的iOS做到這一點,但我不知道有沒有辦法做同樣的watchOS2生成條形碼上觀看OS 2

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

在iOS應用上生成,然後將其轉移到手錶。祝你好運!

+0

我做到了,更新了我的答案謝謝:) –

+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]]; 

}