ios - 将 UIImage 圆角化但使圆的外部透明

标签 ios objective-c core-graphics

目前我正在使用以下代码对 UIImage 进行舍入:

//round the image

    UIImageView *roundView = [[UIImageView alloc] initWithImage:smallImage];
    UIGraphicsBeginImageContextWithOptions(roundView.bounds.size, NO, [UIScreen mainScreen].scale);
    [[UIBezierPath bezierPathWithRoundedRect:roundView.bounds
                                cornerRadius:roundView.frame.size.width/2] addClip];
    [smallImage drawInRect:roundView.bounds];
    UIImage *finalImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    smallImage = finalImage;

    //end round image


    self.thumbnailView.image=smallImage;
    UIImageWriteToSavedPhotosAlbum(smallImage, self, nil, nil); //20kb
    UIGraphicsEndImageContext();

然而,这会使圆圈的外部变白,因此当我按下图像(如果它被用作 UIButton)时,您可以看到它是一个方形图像。无论如何将 UIImage 剪辑成一个圆圈但使其外部透明?任何指针将不胜感激。

最佳答案

你可以设置 UIImageView 的圆角半径,让它像这样变圆:-

[yourImageView.layer setCornerRadius:CGRectGetWidth(yourImageView.frame)/2];

您可以增加它以使其变小。

关于ios - 将 UIImage 圆角化但使圆的外部透明,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29115535/

相关文章:

objective-c - UITableView 阴影

iphone - 如何释放这些泄漏?

ios - 当我们将 nil 指向 sprite 节点时会发生什么?

ios - 在 CGContext 上绘制的部分透明图像周围放置边框

ios - 具有可调整大小图像的 UIButton setImage 与 setBackgroundImage

objective-c - Objective-C : Change Line Width Using UISlider

ios - 在 appdelegate 中以编程方式选择 tableview 单元格不起作用

ios - 仅当成功将对象添加到nsset时,才能将对象添加到nsmutablearray吗?

ios - 应用程序不在标签上显示 var 字符串

objective-c - 即使该应用被终止,仍继续使用NSURLSession下载