Iphone 如何使图像的边缘圆化?

Iphone 如何使图像的边缘圆化?,iphone,objective-c,ios,Iphone,Objective C,Ios,我试图加快UITableView的滚动速度。我通过自己绘制单元而不是添加子视图来实现这一点 我想画的东西之一是图像。图像应具有圆形边缘。当我使用子视图绘制单元时,我改变了UIImageView的图层,使其具有圆角 现在我直接绘制UIImage,没有要修改的图层。如何绘制具有圆边的图像?听起来您只是想删除矩形图像的角:通过CGImage API创建一个新图像-您将在输入图像上应用一个掩码。我很确定这段代码来自stackoverflow - (UIImage*)maskImage:(UIImage

我试图加快UITableView的滚动速度。我通过自己绘制单元而不是添加子视图来实现这一点

我想画的东西之一是图像。图像应具有圆形边缘。当我使用子视图绘制单元时,我改变了UIImageView的图层,使其具有圆角


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

听起来您只是想删除矩形图像的角:通过CGImage API创建一个新图像-您将在输入图像上应用一个掩码。

我很确定这段代码来自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]; 

希望有帮助

在开始此操作之前,请检查是否已将表视图单元格正确出列。这将大大提高性能

例如,用圆角自己绘制图像,并使单元格的背景透明

如果单元格高度可变

使用ResizeableImageWithCapInsets:iOS5.0及更高版本,或使用不推荐的StretcableImageWithLeftCapWidth:topCapHeight:返回图像,该图像将通过重复图像的中心块进行扩展。这也将在执行自动调整大小时保护拐角的曲率。或者

将图像分割成多个片段,为每个片段分别设置UIImageView实例,并为每个片段设置适当的自动调整大小。就性能而言,这是最好的选择,但最终可能会有多达九个插座


您是否对图像使用缓存

如果是这样,我建议您在缓存UIImage之前将其应用于UIImage,这样一旦使用圆角绘制,您就可以重用该图像

这里是UIImage上的一个类别,您可以使用它来获得一个完整的UIImage

我可能在某个地方找到了这段代码,所以我提前向原作者道歉,因为我没有给他/她评分

#import <Foundation/Foundation.h>

@interface UIImage (DPRounded)
- (UIImage *)imageWithCornerRadius:(CGFloat)radius;
@end


@implementation UIImage (DPRounded)

static void addRoundedRectToPath(CGContextRef context, CGRect rect, float ovalWidth, float ovalHeight)
{
    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, 1);
    CGContextAddArcToPoint(context, 0, fh, 0, fh/2, 1);
    CGContextAddArcToPoint(context, 0, 0, fw/2, 0, 1);
    CGContextAddArcToPoint(context, fw, 0, fw, fh/2, 1);
    CGContextClosePath(context);
    CGContextRestoreGState(context);
}

- (UIImage *)imageWithCornerRadius:(CGFloat)radius
{
    UIImage * newImage = nil;

    if(self != nil)
    {
        int w = self.size.width;
        int h = self.size.height;

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

        CGContextBeginPath(context);
        CGRect rect = CGRectMake(0, 0, self.size.width, self.size.height);
        addRoundedRectToPath(context, rect, radius, radius);
        CGContextClosePath(context);
        CGContextClip(context);

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

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

        newImage = [[UIImage imageWithCGImage:imageMasked] retain];
        CGImageRelease(imageMasked);
    }

    return [newImage autorelease];
}

@end