在保留alpha的同时从阴影视图创建UIImage? [英] Create UIImage from shadowed view while retaining alpha?
问题描述
我有一个不寻常的问题。在我的应用程序中,我使用基本的Quartz2d图层阴影来遮蔽 UIImageView
。这是我的代码:
I have a somewhat unusual problem. In my app, I am shadowing a UIImageView
using basic Quartz2d layer shadowing. Here's my code:
imageView.layer.shadowOpacity = 1.0; // Pretty self explanatory
imageView.layer.shadowRadius = 8.0; // My softness
imageView.layer.shadowColor = [UIColor blackColor].CGColor; // Color of the shadow
imageView.layer.shadowOffset = CGSizeMake(0.0, 0.0); // Offset of the shadow
这会在图像视图后面产生很好的模糊效果。但是,我正在使用此视图进行一些动画,并且在过渡期间不断重新计算阴影会导致非常不稳定的过渡。我希望能够做的是从图像视图创建一个UIImage或.png文件,其中模糊和alpha完整。这是我已经尝试过的:
This produces a nice blur behind the image view. However, I am doing some animation with this view, and the constant recalculation of the shadowing during the transitions causes a very choppy transition. What I'd like to be able to do is create a UIImage or .png file out of the image view with the blur and its alpha intact. Here's what I've already tried:
UIGraphicsBeginImageContext(CGSizeMake(320, 396));
[imageView.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UIImageWriteToSavedPhotosAlbum(viewImage, nil, nil, nil);
由于我有一个阴影在图像视图之外,我不能只是通过他在 UIGraphicsBeginImageContext
函数中的大小。我设置了正确的大小,但结果图像不保存我的alpha,而是在阴影后面放置一个白色背景,这对我不起作用,因为我的真实背景是木质纹理。此外,视图不会在结果文件中居中。
Since I have a shadow which "grows outside" of the image view, I can't just pass his size in the UIGraphicsBeginImageContext
function. I set the correct size, but the resulting image doesn't save my alpha, instead it places a white background behind the shadow, which won't work for me because my real background is a wood texture. Also, the view isn't centered in the resulting file.
我对 UIKit
非常满意,但是我是一个真正的noobie与 Quartz
2D图形,所以如果有一个明显的答案,无论如何发送它。 :)
I'm pretty good with UIKit
, but I'm a real noobie with Quartz
2d graphics, so if there's an obvious answer to this, send it anyway. :)
推荐答案
尝试设置 UIImageView
的 backgroundColor
到 [UIColor clearColor]
- 这可以使您当前的解决方案正常工作。
Try setting your UIImageView
's backgroundColor
to [UIColor clearColor]
- this may enable your current solution to work.
imageView.backgroundColor = [UIColor clearColor];
UIGraphicsBeginImageContext(CGSizeMake(320, 396));
[imageView.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
资料来源:在CoreGraphics iphone中将CGLayer转换为*透明* UIImage和PNG文件
这篇关于在保留alpha的同时从阴影视图创建UIImage?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!