Guest User

Untitled

a guest
Oct 16th, 2017
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.89 KB | None | 0 0
  1. - (UIImage*) blankImage:(CGSize)targetSize
  2. {
  3.     UIImage *inputImage = [UIImage imageNamed:@"blank.png"];        // input image to be composited over new image as example
  4.    
  5.     // create a new bitmap image context
  6.    
  7.     NSLog(@"Draw Image Called %f %f", targetSize.width, targetSize.height);
  8.    
  9.     //
  10.     UIGraphicsBeginImageContext(CGSizeMake(targetSize.width, targetSize.height));      
  11.    
  12.     // get context
  13.     //
  14.     CGContextRef context = UIGraphicsGetCurrentContext();      
  15.    
  16.     // push context to make it current
  17.     // (need to do this manually because we are not drawing in a UIView)
  18.     //
  19.     UIGraphicsPushContext(context);                            
  20.    
  21.     // drawing code comes here- look at CGContext reference
  22.     // for available operations
  23.     //
  24.     // this example draws the inputImage into the context
  25.     //
  26.     [inputImage drawInRect:CGRectMake(0, 0, targetSize.width, targetSize.height)];
  27.    
  28.    
  29.     // pop context
  30.     //
  31.     UIGraphicsPopContext();                            
  32.    
  33.     // get a UIImage from the image context- enjoy!!!
  34.     //
  35.     UIImage *outputImage = UIGraphicsGetImageFromCurrentImageContext();
  36.    
  37.     // clean up drawing environment
  38.     //
  39.     UIGraphicsEndImageContext();
  40.    
  41.     return outputImage;
  42. }
  43.  
  44.  
  45. - (void)getRGBComponents:(CGFloat [3])components forColor:(UIColor *)color {
  46.     CGColorSpaceRef rgbColorSpace = CGColorSpaceCreateDeviceRGB();
  47.     unsigned char resultingPixel[4];
  48.     CGContextRef context = CGBitmapContextCreate(&resultingPixel,
  49.                                                  1,
  50.                                                  1,
  51.                                                  8,
  52.                                                  4,
  53.                                                  rgbColorSpace,
  54.                                                  kCGImageAlphaNoneSkipLast);
  55.     CGContextSetFillColorWithColor(context, [color CGColor]);
  56.     CGContextFillRect(context, CGRectMake(0, 0, 1, 1));
  57.     CGContextRelease(context);
  58.     CGColorSpaceRelease(rgbColorSpace);
  59.    
  60.     for (int component = 0; component < 3; component++) {
  61.         components[component] = resultingPixel[component] / 255.0f;
  62.     }
  63. }
  64.  
  65.  
  66.  
  67.  
  68. -(UIImage*) drawQRImage
  69. {
  70.    
  71.     NSLog(@"Draw Image Called %f %f", [theCanvas mySize].width, [theCanvas mySize].height);
  72.    
  73.     // load Image
  74.     //
  75.     //CGImageRef imageRef         = [self blankCGImage];
  76.    
  77.    
  78.     UIImage *image              = [self blankImage:CGSizeMake([theCanvas mySize].width, [theCanvas mySize].height)];    
  79.    
  80.     //UIImage *image = [UIImage imageNamed:@"blank.png"];
  81.     CGImageRef imageRef         = image.CGImage;
  82.    
  83.    
  84.     NSData *data                = (NSData *) CGDataProviderCopyData(CGImageGetDataProvider(imageRef));
  85.     char *pixels                = (char *)[data bytes];
  86.    
  87.     //unsigned long int pixelNr;
  88.    
  89.    
  90.     // manipulate the individual pixels
  91.     //
  92.     //size_t width                = [theCanvas mySize].width;
  93.     //size_t height               = [theCanvas mySize].height;
  94.    
  95.     size_t width                = CGImageGetWidth(imageRef);
  96.     size_t height                 = CGImageGetHeight(imageRef);
  97.    
  98.      
  99.  
  100.     for(int pixelNr = 0; pixelNr < width *  height; pixelNr++){
  101.        
  102.         int value = [[[[[theCanvas frames] objectAtIndex:currentFrame] bitvalues] objectAtIndex:pixelNr] intValue];
  103.        
  104.         UIColor *color = [[[theCanvas pallet] colors] objectAtIndex:value];
  105.        
  106.         CGFloat components[4];
  107.         [self getRGBComponents:components forColor:color];
  108.         //NSLog(@"%f %f %f", components[0], components[1], components[2]);
  109.        
  110.    
  111.        
  112.         int red                 = pixelNr*4;
  113.         int green               = pixelNr*4+1;
  114.         int blue                = pixelNr*4+2;
  115.         int alpha               = 255;
  116.        
  117.         if(color == [UIColor clearColor]){
  118.        
  119.             components[0] = 1;
  120.             components[1] = 1;
  121.             components[2] = 1;
  122.             alpha = 0;
  123.         }
  124.        
  125.        
  126.         // Set Pixel-Color Black
  127.         //
  128.         pixels[blue]         = components[0]*255;
  129.         pixels[green]       = components[1]*255;
  130.         pixels[red]        = components[2]*255;
  131.         pixels[alpha]       = alpha;
  132.        
  133.         //NSLog(@"Bit Value: %d", value);
  134.        
  135.     }
  136.    
  137.  
  138.    
  139.     //
  140.     // [...] deleted some unimportant code here
  141.     //
  142.    
  143.     // create a new image from the modified pixel data
  144.     //
  145.     size_t bitsPerComponent     = CGImageGetBitsPerComponent(imageRef);
  146.     size_t bitsPerPixel         = CGImageGetBitsPerPixel(imageRef);
  147.     size_t bytesPerRow          = CGImageGetBytesPerRow(imageRef);
  148.    
  149.    
  150.     //NSLog(@"bitsPerComponent:%f bitsPerPixel bytesPerRow)
  151.    
  152.     CGColorSpaceRef colorspace  = CGColorSpaceCreateDeviceRGB();
  153.     CGBitmapInfo bitmapInfo     = CGImageGetBitmapInfo(imageRef);
  154.     CGDataProviderRef provider  = CGDataProviderCreateWithData(NULL, pixels, [data length], NULL);
  155.    
  156.     CGImageRef newImageRef      = CGImageCreate (
  157.                                                  width,
  158.                                                  height,
  159.                                                  bitsPerComponent,
  160.                                                  bitsPerPixel,
  161.                                                  bytesPerRow,
  162.                                                  colorspace,
  163.                                                  bitmapInfo,
  164.                                                  provider,
  165.                                                  NULL,
  166.                                                  false,
  167.                                                  kCGRenderingIntentDefault
  168.                                                  );
  169.    
  170.     // the modified image
  171.     //
  172.     UIImage *newImage           = [UIImage imageWithCGImage:newImageRef];
  173.    
  174.     // cleanup
  175.     //
  176.    
  177.     NSLog(@"Draw Image Processing");
  178.    
  179.     //free(pixels);
  180.     //CGImageRelease(imageRef); // DO NOT RELEASE OR ON NEXT RUN NSData GETS EXC_BAD_ACCESS
  181.     //CGColorSpaceRelease(colorspace);
  182.     //CGDataProviderRelease(provider);
  183.     //CGImageRelease(newImageRef);
  184.    
  185.     //QRcode_free(qrCode);
  186.     //QRinput_free(input);
  187.    
  188.     // return created Image
  189.     //
  190.     NSLog(@"Draw Image Finished");
  191.    
  192.     return newImage;
  193. }
Add Comment
Please, Sign In to add comment