我没能找到这一个妥善的解决办法,我需要一个快速的解决方法。下面是一个函数,它给出一个UIImage,返回一个黑暗的alpha填充变暗的新图像。上下文填充命令可以替换为其他绘制或填充例程以提供不同类型的变暗。
这是未优化,并用图形API的最小的知识做出。
您可以使用此功能来设置UIControlStateHighlighted国家形象,这样至少这将是黑暗的。
+ (UIImage *)darkenedImageWithImage:(UIImage *)sourceImage
{
UIImage * darkenedImage = nil;
if (sourceImage)
{
// drawing prep
CGImageRef source = sourceImage.CGImage;
CGRect drawRect = CGRectMake(0.f,
0.f,
sourceImage.size.width,
sourceImage.size.height);
CGContextRef context = CGBitmapContextCreate(NULL,
drawRect.size.width,
drawRect.size.height,
CGImageGetBitsPerComponent(source),
CGImageGetBytesPerRow(source),
CGImageGetColorSpace(source),
CGImageGetBitmapInfo(source)
);
// draw given image and then darken fill it
CGContextDrawImage(context, drawRect, source);
CGContextSetBlendMode(context, kCGBlendModeOverlay);
CGContextSetRGBFillColor(context, 0.f, 0.f, 0.f, 0.5f);
CGContextFillRect(context, drawRect);
// get context result
CGImageRef darkened = CGBitmapContextCreateImage(context);
CGContextRelease(context);
// convert to UIImage and preserve original orientation
darkenedImage = [UIImage imageWithCGImage:darkened
scale:1.f
orientation:sourceImage.imageOrientation];
CGImageRelease(darkened);
}
return darkenedImage;
}
我有一个类似的问题:
那么你可以使用它像图像到从uiimagepicker选择的图片,图像显示旋转90度。 – marciokoko