如何裁剪iOS中圈内的图像

时间:2014-10-01 08:28:37

标签: ios iphone image-processing crop

我正在开展一个项目,我需要在屏幕上显示与enter image description here

相同的屏幕

此处应裁剪图像,该图像仅在圆圈中可见。我尝试过如下图像屏蔽。但它总是在广场上播出。

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

请建议我如何实现这一目标?

2 个答案:

答案 0 :(得分:1)

使用该演示来缩放和裁剪图像圈。Circle Image Crop

用于将图像遮蔽为圆圈,如下所示。

//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 :(得分:0)

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.