简体   繁体   中英

Crop Image using mask

My requirement is to crop the image using the maskImage. Am able the to crop the image but not in the exact ratio as expected. I googled round and tried to implement it but unfortunately didn't got result as expected.This is what am getting after cropping the image.

裁剪前

裁剪后 Following is the code i'm using.

- (UIImage*) maskImage1:(UIImage *) image withMask:(UIImage *) mask
{
CGImageRef imageReference = image.CGImage;
CGImageRef maskReference = mask.CGImage;

CGImageRef imageMask = CGImageMaskCreate(CGImageGetWidth(maskReference),
                                         CGImageGetHeight(maskReference),
                                         CGImageGetBitsPerComponent(maskReference),
                                         CGImageGetBitsPerPixel(maskReference),
                                         CGImageGetBytesPerRow(maskReference),
                                         CGImageGetDataProvider(maskReference),
                                         NULL, // Decode is null
                                         YES // Should interpolate
                                         );

CGImageRef maskedReference = CGImageCreateWithMask(imageReference, imageMask);
CGImageRelease(imageMask);

UIImage *maskedImage = [UIImage imageWithCGImage:maskedReference];
CGImageRelease(maskedReference);

return maskedImage;
}

Thanks..!!

An alternative

You can also achieve the same effect with CALayers, and, in my opinion, is clear.

- (UIImage*) maskImage1:(UIImage *) image withMask:(UIImage *) mask
{
    UIImage* maskedImage = image;

    CALayer *maskLayer = [CALayer layer];

    maskLayer.frame = maskedImage.bounds;
    maskLayer.contents = (__bridge id) mask.CGImage;

    maskedImage.layer.mask = maskLayer;

    return maskedImage;
}

Probably a solution

Your mask UIImage probably has the contentScale wrong

mask.layer.contentScale = [UISCreen mainScreen].scale;

You can also force the size of your mask before you do CGImageMaskCreate:

mask.frame = image.bounds;

Maybe you had already solved this, but as I had the same problem and I solved it, I will explain the solution: The mask is applied to the real size of the photo, in my case it was 3264x2448 and logical it is not the iphone screen size, so when the mask is applied on the image, it became very small. I solved creating a layer on photoshop that have this 3264x2448 size and scaled the mask to stay exactly the same way like on iphone screen.

Other problem that I had is that the image got another orientation when I took the picture(I was using the camera), then I had to turn the mask to the same orientation of the picture on this photoshop layer. Modifying the orientation gives you changing sides, what was height now is width, as the inverse too, so, when I had to calculate the scale, I had to pay attention for which side should be multiplied to get the correct scale.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM