Add a mask to UIImage

Source: Internet
Author: User

Http://stackoverflow.com/questions/17448102/ios-masking-an-image-keeping-retina-scale-factor-in-account

I want to mask a image by passing another image as mask. I am able to mask the image and the resulting image doesn ' t look good. It's jagged at borders.

I guess the problem is related to retina graphics. The scale property for the images is different as:

    1. The image from which I want to mask have a scale value 1. This image generally have a resolution greater than 1000x1000 pixels.
    2. The image according to which I want the resulting image (image had black and white colors only) had scale value 2. This image is generally of resolution 300x300 pixels.

The resulting image has a scale value of 1.

The code I am using is:

+ (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);CGImageRelease(mask);UIImage *maskedImage = [UIImage imageWithCGImage:masked ];CGImageRelease(masked);return maskedImage;}

How can I get a masked image which follows retina scale?

Add a mask to UIImage

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.