How to crop an image entering a circle in iOS

I am working on a project where I need to show a screen like below enter image description here

Here the image should be cropped, which is only visible in the circle. I have tried masking images as shown below. But it is always trimmed with a 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?

+3


source to share


2 answers


Use the demo to scale and crop a circle of images. Trim circle traversal

to mask the image in a circle as shown below.



//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];
}

      

+1


source


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 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.

      



0


source







All Articles