如何在iOS中裁剪圆内的图像
我正在做一个项目,我需要显示一个屏幕,如下所示 在这里,应裁剪图像,该图像仅在圆圈中可见。我已经尝试过如下的图像掩蔽。但它总是呈方形如何在iOS中裁剪圆内的图像,ios,iphone,image-processing,crop,Ios,Iphone,Image Processing,Crop,我正在做一个项目,我需要显示一个屏幕,如下所示 在这里,应裁剪图像,该图像仅在圆圈中可见。我已经尝试过如下的图像掩蔽。但它总是呈方形 - (UIImage*) maskImage1:(UIImage *) image withMask:(UIImage *) mask { CGImageRef imageReference = image.CGImage; CGImageRef maskReference = mask.CGImage; CGImageRef imageMask = CGIma
- (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;
}
请建议我如何实现这一点?使用演示来缩放和裁剪图像圈
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.
用于将您的图像掩蔽成圆形,如下所示
//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];
}
它不会返回我上面提到的圆圈图像。请看,您需要将圆圈对象传递到hiddenView的位置,然后您将获得屏幕截图,因为我已经进行了测试,当我需要特定对象的屏幕截图时,它对我有效。