Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to make UIButton use a masked image correctly?
    text
    copied!<p>I’m creating a UIButton programmatically and then adding a masked image with setImage:</p> <pre><code>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]; </code></pre> <p>The masking is done with this method I got from the web:</p> <pre><code>- (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]; } </code></pre> <p>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.</p> <p>I’m guessing I need to set somekind of BG color to clearColor somewhere, but I’ve tried them all and none work.</p> <p>Any ideas what I might be missing?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload