简体   繁体   English

使用蒙版裁剪图像

[英]Crop Image using mask

My requirement is to crop the image using the maskImage. 我的要求是使用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. 您也可以使用CALayers达到相同的效果,我认为很明显。

- (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 您的蒙版UIImage可能有contentScale错误

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

You can also force the size of your mask before you do CGImageMaskCreate: 您还可以在执行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. 也许您已经解决了这个问题,但是由于我遇到了同样的问题并解决了问题,因此我将说明解决方案:蒙版应用于照片的实际尺寸,在我的情况下为3264x2448,逻辑上不是iphone屏幕尺寸,因此在图像上应用蒙版时,它变得非常小。 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. 我解决了在Photoshop上创建一个具有3264x2448大小的图层的问题,并缩放了蒙版以使其与iPhone屏幕上的显示方式完全相同。

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. 我遇到的另一个问题是,当我拍摄照片时(我正在使用相机),图像具有另一个方向,然后我不得不在此Photoshop图层上将蒙版更改为与图片相同的方向。 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. 修改方向可以改变侧面,现在高度也就是宽度,反之亦然。因此,当我必须计算比例尺时,我必须注意应该乘以哪一侧以获得正确的比例尺。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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