ios - Comparing equality of [UIColor colorWithPatternImage:] -
i want compare 2 uicolors generated using [uicolor colorwithpatternimage:] equality. can't seem figure out how this.
[[uicolor colorwithpatternimage:[uiimage imagenamed:@"camo2"]] isequal: [uicolor colorwithpatternimage:[uiimage imagenamed:@"camo2"]]]
always returns false, whether use == or isequal. know if it's possible compare colorwithpatternimages, or cgpatterns suppose? i've tried comparing cgcolorgetpattern(color.cgcolor) doesn't work either.
edit: reason have function accepts uicolor , gives me nsstring displaying user.
+(nsstring *)colornameforcolor:(uicolor *)color { if ([color isequal:[uicolor whitecolor]]) { return @"white"; } if ([color isequal:[uicolor colorwithpatternimage:[uiimage imagenamed:@"camo"]]]) { return @"camo"; } ... }
is insane thing do? suppose make own object has color property , colorname property...
using private apis
this took reverse engineering of coregraphics
able find 1 private method _cgpatterngetimage
appears return image.
you'll need include following headers:
#include <dlfcn.h> @import coregraphics;
create function pointer:
typedef cgimageref (*cgpatterngetimage)(cgpatternref pattern);
and access function:
-(void)comparepatterns { void *handle = dlopen("/system/library/frameworks/coregraphics.framework/coregraphics", rtld_now); cgpatterngetimage getimage = (cgpatterngetimage) dlsym(handle, "cgpatterngetimage"); uicolor *acolor = [uicolor colorwithpatternimage:[uiimage imagenamed:@"pattern1"]]; uicolor *bcolor = [uicolor colorwithpatternimage:[uiimage imagenamed:@"pattern1"]]; uicolor *ccolor = [uicolor colorwithpatternimage:[uiimage imagenamed:@"pattern2"]]; nsdata *aimagedata = uiimagepngrepresentation([uiimage imagewithcgimage:getimage(cgcolorgetpattern(acolor.cgcolor))]); nsdata *bimagedata = uiimagepngrepresentation([uiimage imagewithcgimage:getimage(cgcolorgetpattern(bcolor.cgcolor))]); nsdata *cimagedata = uiimagepngrepresentation([uiimage imagewithcgimage:getimage(cgcolorgetpattern(ccolor.cgcolor))]); nslog(@"should true: %d",[aimagedata isequal:bimagedata]); nslog(@"should false: %d",[aimagedata isequal:cimagedata]); }
you don't want access private apis in production app might useful testing.
using associative references
if going on app store better solution creating category uicolor
, give associative reference store pattern name or whatever easiest compare. won't compare actual images @ it's possible if don't set correct data identify pattern comparison won't accurate.
include header:
#import <objc/runtime.h>
create category:
@interface uicolor(custompatterns) @property (strong, nonatomic) nsstring* patternname; @end @implementation uicolor(custompatterns) static char custom_patterns_pattern_name_key; @dynamic patternname; -(void)setpatternname:(nsstring *)patternname { objc_setassociatedobject(self, &custom_patterns_pattern_name_key, patternname, objc_association_retain_nonatomic); } -(nsstring *)patternname { return (nsstring*)objc_getassociatedobject(self, &custom_patterns_pattern_name_key); } @end
and can set custom data , compare:
-(void)comparepatterns { uicolor *acolor = [uicolor colorwithpatternimage:[uiimage imagenamed:@"1"]]; acolor.patternname = @"1"; uicolor *bcolor = [uicolor colorwithpatternimage:[uiimage imagenamed:@"1"]]; bcolor.patternname = @"1"; uicolor *ccolor = [uicolor colorwithpatternimage:[uiimage imagenamed:@"2"]]; ccolor.patternname = @"2"; nslog(@"should true: %d",[acolor.patternname isequaltostring:bcolor.patternname]); nslog(@"should false: %d",[acolor.patternname isequaltostring:ccolor.patternname]); }
Comments
Post a Comment