Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get size of repeated pattern from UIColor?

I can query if a UIColor is a pattern by inspecting the CGColor instance it wraps, the CGColorGetPattern() function returns the pattern if it exist, or null if it is not a pattern color.

CGPatternCreate() method requires a bounds when creating a pattern, this value defines the size of the pattern tile (Known as cell in Quartz parlance).

How would I go about to retrieve this pattern size from a UIColor, or the backing CGPattern once it has been created?

like image 528
PeyloW Avatar asked May 13 '11 11:05

PeyloW


4 Answers

If your application is intended for internal distribution only, then you can use a private API. If you look at the functions defined in the CoreGraphics framework, you will see that there is a bunch of functions and among them one called CGPatternGetBounds:

otool -tV /Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS4.3.sdk/System/Library/Frameworks/CoreGraphics.framework/CoreGraphics | egrep "^_CGPattern"

You just have to make some function lookup on the framework and use it through a function pointer.

The header to include:

#include <dlfcn.h>

The function pointer:

typedef CGRect (*CGPatternGetBounds)(CGPatternRef pattern);

The code to retrieve the function:

void *handle = dlopen("/System/Library/Frameworks/CoreGraphics.framework/CoreGraphics", RTLD_NOW);
CGPatternGetBounds getBounds = (CGPatternGetBounds) dlsym(handle, "CGPatternGetBounds");

The code to retrieve the bounds:

UIColor *uicolor = [UIColor groupTableViewBackgroundColor]; // Select a pattern color
CGColorRef color = [uicolor CGColor];
CGPatternRef pattern = CGColorGetPattern(color);
CGRect bounds = getBounds (pattern); // This result is a CGRect(0, 0, 84, 1)
like image 194
Laurent Etiemble Avatar answered Nov 13 '22 04:11

Laurent Etiemble


I dont think its possible to get the bounds from the CGPatternRef, if thats what you're asking

like image 4
SEG Avatar answered Nov 13 '22 05:11

SEG


There does not appear to be any way to directly retrieve any information from the CGPatternRef.

If you must do this, probably the only way (besides poking at the private contents of the CGPattern struct, which probably counts as "using private APIs") is to render the pattern to a sufficiently large image buffer and then detect the repeating subunit. Finding repeating patterns/images in images may be a good starting point for that.

like image 1
Anomie Avatar answered Nov 13 '22 04:11

Anomie


Making your own color class that stores the bounds and let you access them through a property might be a viable solution.

Extracting the pattern bounds from UIColor doesn't seem to be possible.

like image 1
Erik B Avatar answered Nov 13 '22 04:11

Erik B