QRCodeEncoderDemoViewController.mm 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. #import "QRCodeEncoderDemoViewController.h"
  2. @implementation QRCodeEncoderDemoViewController
  3. - (void)dealloc
  4. {
  5. [super dealloc];
  6. }
  7. #pragma mark - View lifecycle
  8. - (void)viewDidLoad
  9. {
  10. [super viewDidLoad];
  11. //the qrcode is square. now we make it 250 pixels wide
  12. int qrcodeImageDimension = 250;
  13. //the string can be very long
  14. NSString* aVeryLongURL = @"http://thelongestlistofthelongeststuffatthelongestdomainnameatlonglast.com/";
  15. //first encode the string into a matrix of bools, TRUE for black dot and FALSE for white. Let the encoder decide the error correction level and version
  16. DataMatrix* qrMatrix = [QREncoder encodeWithECLevel:QR_ECLEVEL_AUTO version:QR_VERSION_AUTO string:aVeryLongURL];
  17. //then render the matrix
  18. UIImage* qrcodeImage = [QREncoder renderDataMatrix:qrMatrix imageDimension:qrcodeImageDimension];
  19. //put the image into the view
  20. UIImageView* qrcodeImageView = [[UIImageView alloc] initWithImage:qrcodeImage];
  21. CGRect parentFrame = self.view.frame;
  22. CGRect tabBarFrame = self.tabBarController.tabBar.frame;
  23. //center the image
  24. CGFloat x = (parentFrame.size.width - qrcodeImageDimension) / 2.0;
  25. CGFloat y = (parentFrame.size.height - qrcodeImageDimension - tabBarFrame.size.height) / 2.0;
  26. CGRect qrcodeImageViewFrame = CGRectMake(x, y, qrcodeImageDimension, qrcodeImageDimension);
  27. [qrcodeImageView setFrame:qrcodeImageViewFrame];
  28. //and that's it!
  29. [self.view addSubview:qrcodeImageView];
  30. [qrcodeImageView release];
  31. }
  32. @end