11

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?

STW
  • 44,917
  • 17
  • 105
  • 161
PeyloW
  • 36,742
  • 12
  • 80
  • 99

4 Answers4

6

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)
Laurent Etiemble
  • 27,111
  • 5
  • 56
  • 81
4

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

SEG
  • 1,717
  • 1
  • 18
  • 33
1

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.

Community
  • 1
  • 1
Anomie
  • 92,546
  • 13
  • 126
  • 145
  • Any hints into how I wold go about and poke into the internals? That would work for apps distributed internally, and not on App Store. – PeyloW May 26 '11 at 17:07
  • You'd either have to find a declaration for struct CGPattern or just inspect the memory in the debugger. And then hope they don't change the layout in a future version of the framework. – Anomie May 26 '11 at 17:40
1

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.

Erik B
  • 40,889
  • 25
  • 119
  • 135