iphone - 如何圆化 UIImage 的边缘?

标签 iphone objective-c ios

我尝试加快 UITableView 的滚动速度。我通过自己绘制单元格而不是添加 subview 来做到这一点。

我想画的东西之一是图像。图像应具有圆角边缘。当我使用 subview 绘制单元格时,我将 UIImageView 的图层更改为圆角。

现在我直接绘制UIImage,没有图层可以修改。如何绘制圆边图像?

最佳答案

我很确定我是从 stackoverflow 获得这段代码的。

- (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 sourceImage = [image CGImage];
CGImageRef imageWithAlpha = sourceImage;
//add alpha channel for images that don't have one (ie GIF, JPEG, etc...)
//this however has a computational cost
// needed to comment out this check. Some images were reporting that they
// had an alpha channel when they didn't! So we always create the channel.
// It isn't expected that the wheelin application will be doing this a lot so 
// the computational cost isn't onerous.
//if (CGImageGetAlphaInfo(sourceImage) == kCGImageAlphaNone) { 
imageWithAlpha = CopyImageAndAddAlphaChannel(sourceImage);
//}

CGImageRef masked = CGImageCreateWithMask(imageWithAlpha, mask);
CGImageRelease(mask);

//release imageWithAlpha if it was created by CopyImageAndAddAlphaChannel
if (sourceImage != imageWithAlpha) {
    CGImageRelease(imageWithAlpha);
}

UIImage* retImage = [UIImage imageWithCGImage:masked];
CGImageRelease(masked);

return retImage;

我称它为:

    customImage = [customImage maskImage:customImage withMask:[UIImage imageNamed:@"CircularMask.png"]];
[customImageView setImage:customImage]; 

希望对您有所帮助!

关于iphone - 如何圆化 UIImage 的边缘?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8431286/

相关文章:

objective-c - 存储稍后在委托(delegate)回调中返回的参数值

ios - 使用现有数据导入 Magical Record

iphone - 我们如何更改表格 View 标题的字体?

iphone - MKMapView 注释更改顺序/顺序

ios - block 如何处理 __weak 引用

objective-c - AFNetworking 发布表单数据

iphone - 存档应用程序时,Xcode 认为我的 Icon@2x.png 适用于 iPad 而不是 iPhone 4

ios - 给定纬度和经度生成快照的最佳方法是什么,真的鼓励跳过 MKMapView

iOS 11 后退按钮问题

iphone - JSONKit 在从 iPhone 中的 Web 服务解码 JSON 对象时更改浮点值