我正在使用此代码来屏蔽带图像的渐变。但它很慢。
CGImageRef gradientMaskImage = CreateGradientImage(1, 500);
CGImageRef masked = CGImageCreateWithMask([[UIImage imageNamed:@"brooklyn_new.jpg"] CGImage], gradientMaskImage);
UIImage *result = [UIImage imageWithCGImage:masked];
CGImageRelease(gradientMaskImage);
CGImageRelease(masked);
return result;
对于CreateGradientImage:
CGImageRef CreateGradientImage(int pixelsWide, int pixelsHigh)
{
CGImageRef theCGImage = NULL;
// gradient is always black-white and the mask must be in the gray colorspace
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceGray();
// create the bitmap context
CGContextRef gradientBitmapContext = CGBitmapContextCreate(NULL, pixelsWide, pixelsHigh,
8, 0, colorSpace, kCGImageAlphaNone);
// define the start and end grayscale values (with the alpha, even though
// our bitmap context doesn't support alpha the gradient requires it)
CGFloat colors[] = {0.0, 1.0, 1.0, 1.0};
// create the CGGradient and then release the gray color space
CGGradientRef grayScaleGradient = CGGradientCreateWithColorComponents(colorSpace, colors, NULL, 2);
CGColorSpaceRelease(colorSpace);
// create the start and end points for the gradient vector (straight down)
CGPoint gradientStartPoint = CGPointZero;
CGPoint gradientEndPoint = CGPointMake(0, pixelsHigh);
// draw the gradient into the gray bitmap context
CGContextDrawLinearGradient(gradientBitmapContext, grayScaleGradient, gradientStartPoint,
gradientEndPoint, kCGGradientDrawsAfterEndLocation);
CGGradientRelease(grayScaleGradient);
// convert the context into a CGImageRef and release the context
theCGImage = CGBitmapContextCreateImage(gradientBitmapContext);
CGContextRelease(gradientBitmapContext);
// return the imageref containing the gradient
return theCGImage;
}
////
CGImageRef gradientMaskImage = CreateGradientImage(1, 500);
这里500将是一个变量。因此,每当我调用具有可变高度的函数时,它将重绘一个蒙版图像。所以它应该非常快。
我怎样才能更快地完成?