我知道可以通过UIImageView实现这一点,但是能否对UIImage进行操作呢? 我想让UIImageView的动画图片数组属性成为相同图像的数组,但具有不同的透明度。 你有什么想法吗?
我知道可以通过UIImageView实现这一点,但是能否对UIImage进行操作呢? 我想让UIImageView的动画图片数组属性成为相同图像的数组,但具有不同的透明度。 你有什么想法吗?
我只需要做这个,但认为Steven的解决方案会很慢。这应该会利用图形硬件。在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
上使用,正是我所需要的。 - NecipAllef基于Alexey Ishkov的答案,但用Swift实现
我使用了UIImage类的扩展。
Swift 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)
Swift 3/4/5:
请注意,此实现返回可选的UIImage。这是因为在Swift 3中,UIGraphicsGetImageFromCurrentImageContext
现在返回一个可选值。如果上下文为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
版本。 - AechoLiu有更简单的解决方案:
- (UIImage *)tranlucentWithAlpha:(CGFloat)alpha
{
UIGraphicsBeginImageContextWithOptions(self.size, NO, self.scale);
[self drawAtPoint:CGPointZero blendMode:kCGBlendModeNormal alpha:alpha];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
嘿,感谢来自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);
我知道现在已经有点晚了,但是我需要类似的功能,因此我设计了一个快速且简单的方法来实现这个功能。
+ (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:
。 - Alexanderimage:withAlpha:
会更好吗? - Jonathan.与其他人相同的结果,不同的风格:
extension UIImage {
func withAlpha(_ alpha: CGFloat) -> UIImage {
return UIGraphicsImageRenderer(size: size).image { _ in
draw(at: .zero, blendMode: .normal, alpha: alpha)
}
}
}
Swift 5:
extension UIImage {
func withAlphaComponent(_ alpha: CGFloat) -> UIImage? {
UIGraphicsBeginImageContextWithOptions(size, false, scale)
defer { UIGraphicsEndImageContext() }
draw(at: .zero, blendMode: .normal, alpha: alpha)
return UIGraphicsGetImageFromCurrentImageContext()
}
}
如果你正在尝试使用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应该避免任何缩放。
希望这个答复能够为初学者节省很多困扰。