Created
March 27, 2014 16:00
-
-
Save bugcloud/9810922 to your computer and use it in GitHub Desktop.
[iOS] QRコードを生成して画像に合成する ref: http://qiita.com/bugcloud/items/3962d5ffdb883021f813
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| platform :ios | |
| pod 'QR-Code-Encoder-for-Objective-C' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //the qrcode is square. now we make it 250 pixels wide | |
| int qrcodeImageDimension = 250; | |
| //the string can be very long | |
| NSString* aVeryLongURL = @"http://thelongestlistofthelongeststuffatthelongestdomainnameatlonglast.com/"; | |
| //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 | |
| DataMatrix* qrMatrix = [QREncoder encodeWithECLevel:QR_ECLEVEL_AUTO version:QR_VERSION_AUTO string:aVeryLongURL]; | |
| //then render the matrix | |
| UIImage* qrcodeImage = [QREncoder renderDataMatrix:qrMatrix imageDimension:qrcodeImageDimension]; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #import "QREncoder.h" | |
| ... | |
| #pragma mark - | |
| #pragma UIImagePickerControllerDelegate | |
| - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info | |
| { | |
| UIImage *image = [info valueForKey:UIImagePickerControllerOriginalImage]; | |
| CGSize size = [image size]; | |
| UIGraphicsBeginImageContext(size); | |
| CGRect rect; | |
| rect.origin = CGPointZero; | |
| rect.size = size; | |
| [image drawInRect:rect]; | |
| //upload image and get its URL | |
| NSString *imageUrl = @"http://xxxxxxxxxx"; | |
| DataMatrix *qrMatrix = [QREncoder encodeWithECLevel:QR_ECLEVEL_AUTO version:QR_VERSION_AUTO string:imageUrl]; | |
| int qrDimention = 640; | |
| UIImage *qrImage = [QREncoder renderDataMatrix:qrMatrix imageDimension:qrDimention]; | |
| //At first, draw white background and then draw QR code | |
| CGContextRef context = UIGraphicsGetCurrentContext(); | |
| CGContextSetFillColorWithColor(context, [UIColor whiteColor].CGColor); | |
| CGContextFillRect(context, CGRectMake(image.size.width - qrDimention - 140.0f, | |
| image.size.height - qrDimention - 140.0f, | |
| qrDimention + 80.0f, | |
| qrDimention + 80.0f)); | |
| [qrImage drawAtPoint:CGPointMake(image.size.width - qrDimention - 100.0f, | |
| image.size.height - qrDimention - 100.0f)]; | |
| UIImage *mergedImage = UIGraphicsGetImageFromCurrentImageContext(); | |
| UIGraphicsEndImageContext(); | |
| image = mergedImage; | |
| ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init]; | |
| [library writeImageToSavedPhotosAlbum:image.CGImage | |
| orientation:(ALAssetOrientation)image.imageOrientation | |
| completionBlock:^(NSURL *assetURL, NSError *error) { | |
| /* Do something */ | |
| }]; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment