我尝试加快 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/