I’m creating a UIButton programmatically and then adding a masked image with setImage:
CGRect photoFrame = CGRectMake(11, 11, 180, 120);
UIButton *cardPhotoButton = [[UIButton alloc] initWithFrame:photoFrame];
cardPhotoButton.backgroundColor = [UIColor clearColor];
UIImage *cardPhoto = [[UIImage alloc] init];
cardPhoto = [self maskImage:
[UIImage imageNamed:[metaDict objectForKey:@"Photo"]] withMask:[UIImage imageNamed:@"CardPhotoFrame.png"]];
[cardPhotoButton setImage:cardPhoto forState:UIControlStateNormal];
cardPhotoButton.contentMode = UIViewContentModeScaleAspectFit;
[self addSubview:cardPhotoButton];
The masking is done with this method I got from the web:
- (UIImage*) maskImage:(UIImage *)image withMask:(UIImage *)maskImage {
CGImageRef maskRef = maskImage.CGImage;
CGImageRef mask = CGImageMaskCreate(CGImageGetWidth(maskRef),
CGImageGetHeight(maskRef),
CGImageGetBitsPerComponent(maskRef),
CGImageGetBitsPerPixel(maskRef),
CGImageGetBytesPerRow(maskRef),
CGImageGetDataProvider(maskRef), NULL, false);
CGImageRef masked = CGImageCreateWithMask([image CGImage], mask);
return [UIImage imageWithCGImage:masked];
}
Now, the problem is that the masking for the default state does not work, but weirdly when I actually click on the button and the button goes to "highlighted" state, the transparent parts of the masked image start to work.
I’m guessing I need to set somekind of BG color to clearColor somewhere, but I’ve tried them all and none work.
Any ideas what I might be missing?