Codec ░ alpha

361Transparent Background of Custom Drawing Class

Usually common problems already have simple solution. Like that one:

Problem
You subclassed UIView, you want to do some custom drawing in drawRect, but no matter what you do or where you draw, the background of the view remains black.

- (void)drawRect:(CGRect)rect {
 // Drawing code
 CGContextRef context = UIGraphicsGetCurrentContext();
 CGContextSetRGBFillColor(context, 0.0, 0.0, 1.0, 1.0);
 CGContextFillEllipseInRect(context, rect);
}

Solution
In the ViewController, which call the drawing class, add

myDrawingClass.opaque = NO;

- or even nicer. In the drawing class’ init function:

self.opaque = NO;

And not like that
- adding “self.opaque = NO;” in the drawRect: function
- CGContextClearRect(context, rect);
- CGContextSetAlpha(context, 0.5f);

355Programmatically capture UIView 338Accessing RGBA Pixel Data 175Curling Images at Twitter 128Adding Alpha Blending Mode to Quartz Composer 37Perform method in Background

338Accessing RGBA Pixel Data


NSData* pixelData = (NSData*)
	CGDataProviderCopyData(CGImageGetDataProvider(c.CGImage));
unsigned char* pixelBytes = (unsigned char *)[pixelData bytes];
for(int i = 0; i < [pixelData length]; i += 4) {
		NSLog(@"pixelBytes[i] R:%i G:%i B:%i A:%i ",
		(int)pixelBytes[i],
		(int)pixelBytes[i+1],
		(int)pixelBytes[i+2],
		(int)pixelBytes[i+3]);
		/*
		pixelBytes[i] = pixelBytes[i+3];
		pixelBytes[i+1] = pixelBytes[i+3];
		pixelBytes[i+2] = pixelBytes[i+3];
		pixelBytes[i+3] = 0;
		 */
    }
	NSData* newPixelData = [NSData dataWithBytes:pixelBytes length:[pixelData length]];
	UIImage* newImage = [UIImage imageWithData:newPixelData];

361Transparent Background of Custom Drawing Class 347UIImage → pixelData → UIImage Rountrip 334Combining Images with UIImage & CGContext – (Offscreen drawing) 246writeToFile – quick file writing 128Adding Alpha Blending Mode to Quartz Composer

128Adding Alpha Blending Mode to Quartz Composer

Courtesy of Kineme.

http://kineme.net/QuartzComposerPatches/GLTools/1.1

361Transparent Background of Custom Drawing Class 338Accessing RGBA Pixel Data 139QC Show/Hide Private Patches 7QC 3.1 Patch Install Location