I'm trying to mask an image (that is in a table view cell) in iOS 8 xcode 7.3 in autoLayout size class mode.
I looked this link: How to Mask an UIImageView
the first answer didn't work for me. The secound answer works, but the size of masked image is incorrect. my code is:
-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
UIImage *imgStory = [UIImage imageNamed:@"story1"];
UIImage *imgMask = [UIImage imageNamed:@"storyFrameMask"];
UIImageView *imgViewStory = (UIImageView *)[cell viewWithTag:100];
UIImageView *imgViewBorder = (UIImageView *)[cell viewWithTag:101];
imgViewStory.image = imgStory;
CALayer *mask = [CALayer layer];
mask.contents = (id)[[UIImage imageNamed:@"storyFrameMask"] CGImage];
// mask.frame = CGRectMake(imgViewBorder.frame.origin.x, imgViewBorder.frame.origin.y, 178.0, 235.0);
mask.frame = CGRectMake(imgViewBorder.frame.origin.x, imgViewBorder.frame.origin.y, imgViewBorder.frame.size.width, imgViewBorder.frame.size.height);
imgViewStory.layer.mask = mask;
imgViewStory.layer.masksToBounds = YES;
}
and my output screen shot is: [![enter image description here][1]][1]
I can fix the problem by giving custom value to width and height of the mask frame (I have commented fixed width and height in my code. you can see it.), and you know this is not the correct way. Because I'm using auto layout and size class.
the fixed width and height works for just a single size class.
This is the scren shot when I set fixed width and height: [![enter image description here][2]][2]
I have searched all the websites but no of them are for auto layout. Any one can help me to have a good mask for all size classes and devices.
Edit: Note that the constraints for my masked imageview is same as the border imageView. so their frame is equal to each other. so the problem is not from my layout. The problem is the masked image that is drown on the masked image view. I can fix the problem by giving custom value to width and height of the mask frame, but this is not the correct way in auto layout.