Codec ░ UIView

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

355Programmatically capture UIView

Ok, a bit late to the party. Apple officially approved the use of UIGetScreenImage().

After carefully considering the issue, Apple is now allowing applications to use the function UIGetScreenImage() to programmatically capture the current screen contents. The function prototype is as follows:

CGImageRef UIGetScreenImage(void);

https://devforums.apple.com/message/149553

How to caputure a view, ideally the live input of the camera? Unfortunaly there’s no clean and clear interface for that. Only the undocumented UIGetScreenCapture() call:

http://www.iphonedevsdk.com/forum/iphone-sdk-development/11219-screenshots-we-allowed-use-uigetscreenimage.html

http://stackoverflow.com/questions/1531815/takepicture-vs-uigetscreenimage

http://svn.saurik.com/repos/menes/trunk/iphonevnc/iPhoneVNC.mm

http://blogs.oreilly.com/iphone/2008/10/creating-a-full-screen-camera.html

But since UIGetScreenCapture() is an undocumented call (in 3.1), here are official ways to get the content of a view.

UIGraphicsGetImageFromCurrentImageContext();

http://icodeblog.com/2009/07/27/1188/

http://www.elrepositorio.com/?p=31

361Transparent Background of Custom Drawing Class 347UIImage → pixelData → UIImage Rountrip