我知道你可以用UIImageView做到这一点,但它可以做到UIImage?我希望将UIImageView的动画图像数组属性设置为具有不同不透明度的相同图像的数组。思考?如何设置UIImage的不透明度/ alpha值?
回答
我只是需要这样做,但认为史蒂文的解决方案会很慢。这应该有希望使用图形硬件。在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];
是啊,我想我可能有一个计时器不断地改变不透明度为像这样做,我想这一定是为动画图像数组属性设置一个数组更容易,因此它可以自己动画。 – Marty 2011-02-22 22:49:57
您可以使用动画代理将动画与'心跳'一起动画。不要使用定时器,动画会顺利地改变alpha。祝你好运。 – 2011-02-22 22:52:27
这只适用于开发人员使用UIImageView的情况。这个问题清楚地表明情况并非如此。 – 2014-08-26 20:29:25
我意识到这是相当晚了,但我需要这样的东西,所以我掀起了一个快速和肮脏的方法来做到这一点。
+ (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:' – Alexander 2012-05-25 09:59:26
如此真实。感谢提示 – 2012-05-28 04:28:19
集通常指的是属性,以某种方式改变接收者的状态。最好命名为'image:withAlpha:'? – 2012-09-02 19:20:18
有很多简单的解决方案:
- (UIImage *)tranlucentWithAlpha:(CGFloat)alpha
{
UIGraphicsBeginImageContextWithOptions(self.size, NO, self.scale);
[self drawAtPoint:CGPointZero blendMode:kCGBlendModeNormal alpha:alpha];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
可能应该是被接受的答案。最快/最有效的方法来获得结果 – 2017-12-14 18:47:31
基于阿列克谢Ishkov的回答,但是在斯威夫特
我用的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:
注意,此实现收益一个可选的UIImage。这是因为在Swift 3中UIGraphicsGetImageFromCurrentImageContext
现在返回一个可选项。如果上下文为零,或者不使用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
}
}
要使用:从Xamarin用户
let image = UIImage(named: "my_image")
let transparentImage = image?.image(alpha: 0.5)
我更喜欢这个'Swift 3'版本。 – AechoLiu 2017-11-03 08:28:28
嘿,嘿,谢谢! :)这里有云转换为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);
- 1. 如何去除不透明度,但保持UIImage的alpha通道?
- 2. 如何设置UIImage的Alpha?
- 3. Imagick ALPHA VS不透明度
- 4. OpenCV:如何设置一个像素的Alpha透明度
- 5. 如何设置光谱透明度
- 6. Renderscript透明度/ alpha
- 7. 设置不透明度ScrollPane
- 8. 获取版面的Alpha /不透明度
- 9. R ggplot geom_hex alpha透明度
- 10. 透明UIImage
- 11. 如何在Android中为布局背景设置不透明度(Alpha)?
- 12. 如何在XAML中设置背景不透明度和边框不透明度?
- 13. 重置继承的不透明度/过滤器:alpha到完全透明
- 14. 设置的LayoutParams透明度
- 15. IOS:在UIImage中设置alpha
- 16. 如何设置Bing Map API层的不透明度,但保留PushPins对象的完全不透明度?
- 17. 如何获得不透明度值作为alpha滤镜的int值?
- 18. 的JLabel不透明和Alpha
- 19. 更改UIView的alpha值会影响字体的不透明度
- 20. 设置可绘制的不透明度
- 21. 如何逐步透明UIImage?
- 22. 设置图像透明度
- 23. 设置CSS中不透明度的div内元素的不透明度?
- 24. 透明的UIImage不是navrar中的透明
- 25. 如何设置HUD窗口的透明度
- 26. 将alpha设置为零不会使我的标签透明
- 27. 如何设置而QListView透明度,setWindowOpacity不到风度工作
- 28. 设置图像不透明度问题
- 29. 将不透明度设置为ImageButton?
- 30. 设置不透明度为形状
甚至不记得为什么我需要这个:) – Marty 2012-05-31 02:48:15
伟大的解决方案!谢谢尼克! – Christopher 2012-06-18 22:00:57
只需确保您在主线程上调用UIGraphicsBeginImageContextWithOptions,因为背景渲染将是不可预知的。 – 2012-11-15 09:31:50