我知道您可以使用UIImageView来做到这一点,但是可以对UIImage做到吗?我想让UIImageView的动画图像array属性成为相同图像但不透明的数组。有什么想法吗?
Answers:
我只需要这样做,但认为史蒂文的解决方案很慢。希望可以使用图形硬件。在UIImage上创建一个类别:
- (UIImage *)imageByApplyingAlpha:(CGFloat) alpha {
UIGraphicsBeginImageContextWithOptions(self.size, NO, 0.0f);
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGRect area = CGRectMake(0, 0, self.size.width, self.size.height);
CGContextScaleCTM(ctx, 1, -1);
CGContextTranslateCTM(ctx, 0, -area.size.height);
CGContextSetBlendMode(ctx, kCGBlendModeMultiply);
CGContextSetAlpha(ctx, alpha);
CGContextDrawImage(ctx, area, self.CGImage);
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}
设置显示它的视图的不透明度。
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageWithName:@"SomeName.png"]];
imageView.alpha = 0.5; //Alpha runs from 0.0 to 1.0
在动画中使用它。您可以在动画中更改Alpha的持续时间。
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:1.0];
//Set alpha
[UIView commitAnimations];
UIButton
,正是我需要的。
基于Alexey Ishkov的答案,但基于Swift
我使用了UIImage类的扩展。
斯威夫特2:
UIImage扩展:
extension UIImage {
func imageWithAlpha(alpha: CGFloat) -> UIImage {
UIGraphicsBeginImageContextWithOptions(size, false, scale)
drawAtPoint(CGPointZero, blendMode: .Normal, alpha: alpha)
let newImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return newImage
}
}
使用方法:
let image = UIImage(named: "my_image")
let transparentImage = image.imageWithAlpha(0.5)
迅捷3/4/5:
请注意,此实现返回一个可选的UIImage。这是因为在Swift 3中UIGraphicsGetImageFromCurrentImageContext
现在返回了一个可选的。如果上下文为nil或未使用创建的上下文,则此值为nil UIGraphicsBeginImageContext
。
UIImage扩展:
extension UIImage {
func image(alpha: CGFloat) -> UIImage? {
UIGraphicsBeginImageContextWithOptions(size, false, scale)
draw(at: .zero, blendMode: .normal, alpha: alpha)
let newImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return newImage
}
}
使用方法:
let image = UIImage(named: "my_image")
let transparentImage = image?.image(alpha: 0.5)
Swift 3
版本。
有更简单的解决方案:
- (UIImage *)tranlucentWithAlpha:(CGFloat)alpha
{
UIGraphicsBeginImageContextWithOptions(self.size, NO, self.scale);
[self drawAtPoint:CGPointZero blendMode:kCGBlendModeNormal alpha:alpha];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
我意识到这已经很晚了,但是我需要这样的东西,所以我想出了一种快速而肮脏的方法来做到这一点。
+ (UIImage *) image:(UIImage *)image withAlpha:(CGFloat)alpha{
// Create a pixel buffer in an easy to use format
CGImageRef imageRef = [image CGImage];
NSUInteger width = CGImageGetWidth(imageRef);
NSUInteger height = CGImageGetHeight(imageRef);
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
UInt8 * m_PixelBuf = malloc(sizeof(UInt8) * height * width * 4);
NSUInteger bytesPerPixel = 4;
NSUInteger bytesPerRow = bytesPerPixel * width;
NSUInteger bitsPerComponent = 8;
CGContextRef context = CGBitmapContextCreate(m_PixelBuf, width, height,
bitsPerComponent, bytesPerRow, colorSpace,
kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);
CGContextDrawImage(context, CGRectMake(0, 0, width, height), imageRef);
CGContextRelease(context);
//alter the alpha
int length = height * width * 4;
for (int i=0; i<length; i+=4)
{
m_PixelBuf[i+3] = 255*alpha;
}
//create a new image
CGContextRef ctx = CGBitmapContextCreate(m_PixelBuf, width, height,
bitsPerComponent, bytesPerRow, colorSpace,
kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);
CGImageRef newImgRef = CGBitmapContextCreateImage(ctx);
CGColorSpaceRelease(colorSpace);
CGContextRelease(ctx);
free(m_PixelBuf);
UIImage *finalImage = [UIImage imageWithCGImage:newImgRef];
CGImageRelease(newImgRef);
return finalImage;
}
setImage:withAlpha:
image:withAlpha:
吗?
嘿,感谢Xamarin用户!:)在这里它被翻译成c#
//***************************************************************************
public static class ImageExtensions
//***************************************************************************
{
//-------------------------------------------------------------
public static UIImage WithAlpha(this UIImage image, float alpha)
//-------------------------------------------------------------
{
UIGraphics.BeginImageContextWithOptions(image.Size,false,image.CurrentScale);
image.Draw(CGPoint.Empty, CGBlendMode.Normal, alpha);
var newImage = UIGraphics.GetImageFromCurrentImageContext();
UIGraphics.EndImageContext();
return newImage;
}
}
用法示例:
var MySupaImage = UIImage.FromBundle("opaquestuff.png").WithAlpha(0.15f);
如果您正在试验Metal渲染,并且要在第一个答复中提取由imageByApplyingAlpha生成的CGImage,则最终可能会得到比您期望的更大的Metal渲染。在试用Metal时,您可能需要更改imageByApplyingAlpha中的一行代码:
UIGraphicsBeginImageContextWithOptions (self.size, NO, 1.0f);
// UIGraphicsBeginImageContextWithOptions (self.size, NO, 0.0f);
如果您使用的缩放比例为3.0的设备(如iPhone 11 Pro Max),则上面显示的0.0缩放比例将为您提供比预期大三倍的CGImage。将比例因子更改为1.0应该避免任何缩放。
希望此答复将为初学者节省很多麻烦。