【问题标题】:How to crop an image which is coming inside circle in iOS如何裁剪在iOS中进入圆圈内的图像
【发布时间】:2014-11-26 01:07:01
【问题描述】:

我正在做一个项目,我需要显示与以下相同的屏幕

此处应裁剪图像,该图像仅在圆圈中可见。我尝试了如下的图像遮罩。但它总是按正方形裁剪。

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

请建议我怎样才能做到这一点?

【问题讨论】:

标签: ios iphone image-processing crop


【解决方案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];
}

【讨论】:

    【解决方案2】:
    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.
    

    【讨论】:

    • 它没有返回圆形图像
    • 我上面已经提到了
    • 看,你需要传递你的圆形对象来代替 hiddenView 然后你会得到你的屏幕截图,因为我已经测试过,当我想要一个特定对象的屏幕截图时它对我有用。
    猜你喜欢
    • 1970-01-01
    • 2020-04-16
    • 2015-09-22
    • 1970-01-01
    • 2011-06-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多