简体   繁体   English

如何裁剪iOS中圈内的图像

[英]How to crop an image which is coming inside circle in iOS

I am working on a project where I need to show an screen same as below 我正在开展一个项目,我需要显示与下面相同的屏幕 在此输入图像描述

Here the image should be cropped which is visible only in the circle. 此处应裁剪图像,该图像仅在圆圈中可见。 I have tried image masking as below. 我尝试过如下图像屏蔽。 But it always crop in square. 但它总是在广场上播种。

- (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;
}

Please suggest how can I achieve this? 请建议我如何实现这一目标?

Use the demo to scale and crop image circle. 使用该演示来缩放和裁剪图像圈。 Circle Image Crop 圆形图像裁剪

for Masking your image in to circle as below cede. 用于将图像置于圆圈中,如下所示。

//Masking the image
- (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];
}
UIGraphicsBeginImageContextWithOptions(hiddenView.bounds.size, self.view.opaque, 0.0); //In this I have take screenshot of a hiddenView that I have added from IB with a background color anything( in this case it's orange). 
//Replace this hiddenView object with your object of whom you want to take screenshot.

[hiddenView.layer renderInContext:UIGraphicsGetCurrentContext()];  //Similarly replace hiddenView here with your object.

UIImage*theImage=UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

NSData*theImageData=UIImageJPEGRepresentation(theImage, 1.0 );
imgView.image =  [UIImage imageWithData:theImageData];  //I placed a UIImageView to check and place the screenshot into it as Image ,simply to cross check if I'm getting a right screenshot or not.
 //So you could also remove this line after your have verified that your getting right screen shot.

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

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