简体   繁体   English

在iOS上具有圆角和透明背景的Quartz UIImage

[英]Quartz UIImage with rounded corners and transparent background on iOS

I have used this solution to apply corner radius to UIImage in Quartz . 我已使用此解决方案将角半径应用于Quartz UIImage It works fine like I predicted. 如我所料,它运作良好。

However the corners area outside of the image are not transparent but somewhat colored in white: 但是,图像外部的角区域不是透明的,而是有些白色:

石英制带圆角的UIImage

The image above shows top left corner of processed image that is opaque and not transparent. 上图显示了处理后图像的左上角,该图像是不透明且不透明的。

I want cropped-out edges to be totally transparent. 我希望裁剪的边缘完全透明。 How can I achieve this? 我该如何实现?

EDIT: If I want to do this with UIImageView I would already done this. 编辑:如果我想用UIImageView做到这一点,我已经做到了。 So please keep in mind that I want to do this with Quartz on UIImage object not UIImageView . 所以请记住,我要在UIImage对象而不是UIImageView上使用Quartz进行此操作。

SOLUTION: The problem is actually not in the drawing code but rather in writing that image to the file system. 解决方案:问题实际上不在绘图代码中,而是在将该图像写入文件系统中。 I've saved image as JPEG an not as PNG. 我已将图像另存为JPEG,而不是PNG。 That's why corners were not transparent cos JPEG does not have alpha filter. 这就是为什么角落不透明的原因cos JPEG没有alpha滤镜。

I usually use a custom category implemented on UIImage to crop the images with round corners. 我通常使用在UIImage上实现的自定义类别来裁剪带有圆角的图像。 This was taken from this answer. 这是从这个答案中得出的。

void addRoundedRectToPath(CGContextRef context, CGRect rect, float ovalWidth, float ovalHeight, BOOL top, BOOL bottom)
{
    float fw, fh;
    if (ovalWidth == 0 || ovalHeight == 0) {
        CGContextAddRect(context, rect);
        return;
    }
    CGContextSaveGState(context);
    CGContextTranslateCTM (context, CGRectGetMinX(rect), CGRectGetMinY(rect));
    CGContextScaleCTM (context, ovalWidth, ovalHeight);
    fw = CGRectGetWidth (rect) / ovalWidth;
    fh = CGRectGetHeight (rect) / ovalHeight;
    CGContextMoveToPoint(context, fw, fh/2);
    CGContextAddArcToPoint(context, fw, fh, fw/2, fh, 0);

    NSLog(@"bottom? %d", bottom);

    if (top) {
        CGContextAddArcToPoint(context, 0, fh, 0, fh/2, 3);
    } else {
        CGContextAddArcToPoint(context, 0, fh, 0, fh/2, 0);
    }

    if (bottom) {
        CGContextAddArcToPoint(context, 0, 0, fw/2, 0, 3);
    } else {
        CGContextAddArcToPoint(context, 0, 0, fw/2, 0, 0);
    }

    CGContextAddArcToPoint(context, fw, 0, fw, fh/2, 0);
    CGContextClosePath(context);
    CGContextRestoreGState(context);
}

- (UIImage *)roundCornersOfImage:(UIImage *)source roundTop:(BOOL)top roundBottom:(BOOL)bottom {
    int w = source.size.width;
    int h = source.size.height;

    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    CGContextRef context = CGBitmapContextCreate(NULL, w, h, 8, 4 * w, colorSpace, kCGImageAlphaPremultipliedFirst);

    CGContextBeginPath(context);
    CGRect rect = CGRectMake(0, 0, w, h);
    addRoundedRectToPath(context, rect, 4, 4, top, bottom);
    CGContextClosePath(context);
    CGContextClip(context);

    CGContextDrawImage(context, CGRectMake(0, 0, w, h), source.CGImage);

    CGImageRef imageMasked = CGBitmapContextCreateImage(context);
    CGContextRelease(context);
    CGColorSpaceRelease(colorSpace);

    return [UIImage imageWithCGImage:imageMasked];    
}

For transparent use this: 为了transparent使用此:

 const float colorMasking[6] = {1.0, 1.0, 0.0, 0.0, 1.0, 1.0};
 yourImage = [UIImage imageWithCGImage: CGImageCreateWithMaskingColors(yourImage.CGImage, colorMasking)];

EDIT : Add this both line in method of UIImage category 编辑 :在UIImage类别的方法中添加这两行

CGContextRef context = UIGraphicsGetCurrentContext();
CGContextClearRect(context, rect);

add these bellow lines in your drawRect method .. 将这些波纹管添加到您的drawRect方法中。

- (void)drawRect:(CGRect)rect {
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextClearRect(context, rect);
    ///your another code here
}

after create Context it required to clear another context from that Rect... so after that line add this line CGContextClearRect(context, rect); 创建上下文后,它需要从该Rect中清除另一个上下文...因此,在该行之后添加此行CGContextClearRect(context, rect); .

The problem is actually not in the drawing code but rather in writing that image to the file system. 问题实际上不在绘图代码中,而在于将该图像写入文件系统中。 I've saved image as JPEG an not as PNG. 我已将图像另存为JPEG,而不是PNG。 That's why corners were not transparent cos JPEG does not have alpha filter. 这就是为什么角落不透明的原因cos JPEG没有alpha滤镜。

Just replaced this line of code: 刚刚替换了以下代码:

[UIImageJPEGRepresentation(image, 0.75) writeToFile:path atomically:YES];

... with this one: ...与此:

[UIImagePNGRepresentation(image) writeToFile:path atomically:YES];

Try this code 试试这个代码

Image.layer.cornerRadius = 10.0;
Image.layer.borderColor = [UIColor blackColor].CGColor;
Image.layer.borderWidth = 0.5;
Image.clipsToBounds = YES;
Image.backgroundColor = [UIColor clearColor];

Hope it helps you 希望对您有帮助

EDIT :- 编辑:-

I think you just need to add this :- 我认为您只需要添加:-

Image.clipsToBounds = YES;

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

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